简体   繁体   English

在C#中拆分字符串

[英]Splitting a string in C#

Let's say I have this string: 假设我有这个字符串:

"param1,r:1234,p:myparameters=1,2,3" 

...and I would like to split it into: ......我想把它分成:

param1
r:1234
p:myparameters=1,2,3

I've used the split function and of course it splits it at every comma. 我已经使用了分割功能,当然它会在每个逗号分割它。 Is there a way to do this using regex or will I have to write my own split function? 有没有办法使用正则表达式执行此操作,还是我必须编写自己的拆分函数?

Personally, I would try something like this: 就个人而言,我会尝试这样的事情:

,(?=[^,]+:.*?)

Basically, use a positive look-ahead to find a comma, followed by a "key-value" pair (this defined by a key, a colon, and more information [data] (including other commas). This should disqualify the commas between the numbers, too. 基本上,使用正向前瞻来查找逗号,然后是“键值”对(由键,冒号和更多信息[数据](包括其他逗号)定义。这应该取消逗号之间的逗号。数字也是。

You can use ; 你可以用; for separating values which makes easy to work with it. 用于分离易于使用的值。

Since you have , for separation and also for values it is difficult to split it. 既然你有,分离,也为价值就很难把它分解。

You have 你有

string str = "param1,r:1234,p:myparameters=1,2,3"

Recommended to use 建议使用

string str = "param1;r:1234;p:myparameters=1,2,3"

which can be splited as 可以分裂为

var strArray = str.Split(';');

strArray[0];  // contains param1
strArray[1];  // r:1234
strArray[2];  // p:myparameters=1,2,3

I'm not sure how you would write a split that knew which commas to split on there, honestly. 老实说,我不确定你怎么写一个知道哪些逗号分裂在那里的分裂。

Unless it's a fixed number each time in which case, just use the String.Split overload that takes an int specifying how many substrings to return at max 除非在每种情况下每次都是固定数,否则只需使用String.Split重载,该重载采用int指定最多返回多少个子字符串

If you're going to have comma-delimited data that's not always a fixed number of items and it could have literal commas in the data itself, they really should be quoted. 如果您要使用逗号分隔的数据并不总是固定数量的项目, 并且数据本身可能包含文字逗号,则应该引用它们。 If you can control the input in any way, you should encourage that, and use an actual CSV parser instead of String.Split 如果您可以以任何方式控制输入,您应该鼓励它,并使用实际的CSV解析器而不是String.Split

That depends. 那要看。 You can't parse it with regex (or anything else) unless you can identify a consistent rule separating one group from another. 您无法使用正则表达式(或其他任何内容)对其进行解析,除非您可以确定将一个组与另一个组分开的一致规则。 Based on your sample, I can't clearly identify such a rule (though I have some guesses). 根据你的样本,我无法清楚地确定这样的规则(虽然我有一些猜测)。 How does the system know that p:myparameters=1,2,3 is a single item? 系统如何知道p:myparameters=1,2,3是单个项目? For example, if there were another item after it, what would be the difference between that and the 1,2,3 ? 例如,如果之后还有另一个项目,那么它与1,2,3之间会有什么区别? Figure that out and you'll be pretty close to a solution. 想出来,你将非常接近解决方案。

If you're able to change the format of the input string, why not decide on a consistent delimiter between your groups? 如果您能够更改输入字符串的格式,为什么不决定组之间的一致分隔符? ; would be a good choice. 将是一个不错的选择。 Use an input like param1;r:1234;p:myparameters=1,2,3 and there will be no ambiguity where the groups are, plus you can just split on ; 使用像param1;r:1234;p:myparameters=1,2,3这样的输入,组中没有歧义,加上你可以分开; and you won't need regex. 你不需要正则表达式。

The simplest approach would be changing your delimiter from "," to something like "|". 最简单的方法是将分隔符从“,”更改为“|”。 Then you can split on "|" 然后你可以拆分“|” no problem. 没问题。 However if you can't change the delimiting character then maybe you could encode the sections in a fashion similar to CSV. 但是,如果您无法更改分隔符,那么您可以使用类似于CSV的方式对这些部分进行编码。

CSV files have the same issue... the standard there is to put double quotes "" around columns. CSV文件具有相同的问题......标准是在列周围放置双引号“”。

For example, your string would be "param1","r:1234","p:myparameters=1,2,3". 例如,您的字符串将是“param1”,“r:1234”,“p:myparameters = 1,2,3”。

Then you could use the Microsoft.VisualBasic.FileIO.TextFieldParser to split/parse. 然后,您可以使用Microsoft.VisualBasic.FileIO.TextFieldParser进行拆分/解析。 You can include this in c# even though its in the VisualBasic namespace. 你可以在c#中包含它,即使它在VisualBasic命名空间中。

TextFieldParser TextFieldParser

你的意思是:string [] str = System.Text.RegularExpression.Regex.Spilt(“param1,r:1234,p:myparameters = 1,2,3”,@“\\,”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM