可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using the following code to split a string:
string sss="125asdasdlkmlkdfknkldj125kdjfngdkjfndkg125ksndkfjdks125"; List s = new List(sss.Split("125"));
However, I receive a compile time error:
cannot convert from 'string' to 'char[]'
What is the correct way to split a string by another string?
回答1:
There is no overload for String.Split which takes just a string, instead use the next closest match:
List s = new List( sss.Split(new string[] { "125" }, StringSplitOptions.None);
回答2:
You can just create a char []:
List s = new List(sss.split(new char[] {'1', '2', '5'}))
or
List s = new List(sss.split("125".ToCharArray()));
More information: http://msdn.microsoft.com/en-us/library/ezftk57x.aspx
回答3:
That depends on what you wish to achieve. If you wish to split at the string "125" then do
sss.split(new[]{"125"},StringSplitOptions.RemoveEmptyEntries); //or StringSplitOptions.None
if you wish to split at any occurrence of 1, 2 or 5 then do
sss.split(new[]{'1','2','5'});
回答4:
Use a string array:
sss.Split(new[]{"125"},StringSplitOptions.None)
Or StringSplitOptions.RemoveEmptyEntries if you don't want a blank string for before the first 125.
回答5:
Try this:
//string = "friend&mom&dad&brother" string.Split('&')[int]; //string.Split('&')[0] = "friend"
//string.Split('&')[1] = "mom"
//string.Split('&')[2] = "dad"
//string.Split('&')[3] = "brother"