Cannot convert from 'string' to 'char []' for split

I use the following code to split the string:

string sss="125asdasdlkmlkdfknkldj125kdjfngdkjfndkg125ksndkfjdks125";

List<String> s = new List<String>(sss.Split("125"));

However, I get a compile-time error:

cannot convert from 'string' to 'char []'

What is the correct way to split a string into another string?

+5
source share
5 answers

There is no overload for which takes all , use the following closest match instead: String.Splitstring

List<string> s = new List<string>(
    sss.Split(new string[] { "125" }, StringSplitOptions.None);
+23
source

You can simply create char []:

 List<String> s = new List<String>(sss.split(new char[] {'1', '2', '5'}))

or

 List<String> s = new List<String>(sss.split("125".ToCharArray()));

Additional information: http://msdn.microsoft.com/en-us/library/ezftk57x.aspx

+6
source

, . "125",

sss.split(new[]{"125"},StringSplitOptions.RemoveEmptyEntries); //or StringSplitOptions.None

if you want to break 1, 2 or 5 anyway, then do

sss.split(new[]{'1','2','5'}); 
+2
source

Use an array of strings:

sss.Split(new[]{"125"},StringSplitOptions.None)

Or StringSplitOptions.RemoveEmptyEntries, if you do not need an empty string before the first 125.

+1
source

Try the following:

//string = "friend&mom&dad&brother"
  string.Split('&')[int];
//string.Split('&')[0] = "friend"<br>
//string.Split('&')[1] = "mom"<br>
//string.Split('&')[2] = "dad"<br>
//string.Split('&')[3] = "brother"<br>
0
source

All Articles