C# Split A String By Another String
I've been using the Split()
method to split strings, but this only appears to work if you are splitting a string by a character. Is there any way to split a string
, with another string being the split by parameter? I've tried converting the splitter into a character array, with no luck.
In other words, I'd like to split the string
:
THExxQUICKxxBROWNxxFOX
by xx
, and return an array with values:
THE, QUICK, BROWN, FOX
为了分割一个字符串,你必须使用字符串数组重载。
string data = "THExxQUICKxxBROWNxxFOX";
return data.Split(new string[] { "xx" }, StringSplitOptions.None);
There is an overload of Split that takes strings.
"THExxQUICKxxBROWNxxFOX".Split(new [] { "xx" }, StringSplitOptions.None);
You can use either of these StringSplitOptions
So if the string is "THExxQUICKxxxxBROWNxxFOX", StringSplitOptions.None
will return an empty entry in the array for the "xxxx" part while StringSplitOptions.RemoveEmptyEntries
will not.
Regex.Split(string,"xx")
is the way I do it usually. Of course you'll need a
using System.Text.RegularExpressions;
but than again I need that lib all the time.
链接地址: http://www.djcxy.com/p/19558.html上一篇: 使用字符串分隔符(标准C ++)在C ++中解析(拆分)字符串
下一篇: C#将字符串拆分为另一个字符串