C#中如何分割字符串
我有一个类似
"List_1 fooo asdf List_2 bar fdsa XList_3 fooo bar"
List
的字符串,就像
List<String> l_lstValues = new List<string> { "List_1", "XList_3", "List_2" };
我需要根据 l_lstValues
中的值拆分字符串一样。
所以分割的子字符串就像
List_1 fooo asdf
List_2 bar fdsa
XList_3 fooo bar
请给我一个方法来做到这一点 提前致谢
I'm having a string like
"List_1 fooo asdf List_2 bar fdsa XList_3 fooo bar"
and a List<String>
like
List<String> l_lstValues = new List<string> { "List_1", "XList_3", "List_2" };
I need to split the string based on the value in the l_lstValues
.
So the splitted substrings will be like
List_1 fooo asdf
List_2 bar fdsa
XList_3 fooo bar
Please post me a way to do this
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你可以这样做:
所以在这里,每当我遇到列表中的元素时,我都会插入一个
~
(第一个元素除外,因此外部e.MoveNext()
)然后分割~
(注意前面的空格)最大的假设是字符串中没有~
,但我认为这个解决方案足够简单,如果你可以找到这样的角色并确保该角色不会出现在原作中 细绳。如果字符不适合您,请使用类似~~@@
的内容,因为我的解决方案显示用string[]
分割字符串,您只需添加整个字符串即可分裂。当然你可以这样做:
但这将有一个空字符串,我只是喜欢使用
MoveNext()
和Current
:)You can do something like this:
So here, I insert a
~
whenever I encounter a element from the list ( except for the first, hence the outsidee.MoveNext()
) and then split on~
( note the preceding space) The biggest assumption is that you don't have~
in the string, but I think this solution is simple enough if you can find such a character and ensure that character won't occur in the original string. If character doesn't work for you, use something like~~@@
and since my solution shows string split withstring[]
you can just add the entire string for the split.Of course you can do something like:
but that will have an empty string, and I just like using
MoveNext()
andCurrent
:)您可以使用添加的控制字符替换原始字符串中列表中的每个字符串,然后根据该字符进行拆分。例如,您的原始字符串:
需要变为:
稍后将根据
;
进行拆分,产生所需的结果。为此,我使用此代码:您还可以汇编以下正则表达式:
第一个标记
(\S)
将阻止替换第一个出现的位置,第二个标记\s? 将删除空格。现在我们用它来添加
;
:正则表达式选项有点危险,因为单词可以包含 scape 序列。
You can replace every string of the list in the original string with an added control character, and then split on that caracter. For instance, your original string:
Need to become:
Which will later be split based on
;
, producing the desired result. For that, i use this code:You could also assemble the following regular expression:
The first token
(\S)
will prevent replacing the first occurrence, and the second token\s?
will remove the space. Now we use it to add the;
:The regex option is a bit more dangerous because the words can contain scape sequences.
您可以执行如下操作:
编辑:修改为打印带有列表单词的片段
假设:<中没有'
:
'代码>sampleStrYou could do something like below:
EDIT: Modified to print the fragments with list word as well
Assumption: There is no '
:
' insampleStr
这是最简单直接的解决方案:
该算法没有什么特别之处:它迭代所有传入的单词并跟踪“当前键”,该“当前键”用于将相应的值排序到字典中。
编辑:我简化了原始答案以更匹配问题。它现在返回一个 string[] 数组 - 就像 String.Split() 方法一样。如果传入字符串的序列不是以 l_lstValues 列表中的键开头,则会引发异常。
Here is the most simple and straight-forward solution:
There is nothing special about the algorithm: it iterates all incoming words and tracks a 'current key' which is used to sort corresponding values into the dictionary.
EDIT: I simplyfied the original answer to more match the question. It now returns a string[] array - just like the String.Split() method does. An exception will be thrown, if the sequence of incoming strings does not start with a key out of the l_lstValues list.
您可以使用 String.IndexOf 方法来获取起始索引列表中每个短语的字符。
然后,您可以使用该索引来分割字符串。
获得所有索引后,对它们进行排序:
然后,使用它们获取结果字符串:
You can use the String.IndexOf method to get the index of the starting character for each phrase in your list.
Then, you can use this index to split the string.
Once you have all the indices, sort them:
And then, use them to get resulting strings:
这是我制作的示例代码。如果您的键拆分器按顺序位于原始字符串的从左到右,这将获得您需要的子字符串。
Here is a sample code i made. This will get the substring you need provided that your key splitters are sequentially located from left to right of your original string.
您可以使用以下代码来完成此任务
You can use following code achieving this task
你必须在msdn上使用这个split方法,你必须将你的List传递到一个数组中,然后,你必须作为split该数组的参数传递。
我将链接留在这里
http://msdn。 microsoft.com/en-us/library/tabh47cf(v=VS.90).aspx
如果您想保留要分割的单词,则必须迭代结果数组,然后添加单词在您的列表中,如果字符串和列表中的顺序相同。
如果顺序未知,则必须使用indexOf来定位列表中的单词并手动拆分字符串。
再见
You have to use this split method on msdn, you have to pass your List into an array and then, you have to pass as a parameter of the split that array.
I leave you the link here
http://msdn.microsoft.com/en-us/library/tabh47cf(v=VS.90).aspx
If you want to mantain the words you're splitting with, you will have to iterate the resulted array and then add the words in your list, if you have the same order in the string and in the list.
If the order is unknown, you mus use indexOf to locate the words in the list and split the string manually.
See you