Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am using the following code to split a string:

string sss="125asdasdlkmlkdfknkldj125kdjfngdkjfndkg125ksndkfjdks125";  List<String> s = new List<String>(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?

like image 363
Csharp Csh Avatar asked Nov 08 '11 19:11

Csharp Csh


People also ask

What is new string in C#?

The String. Insert method creates a new string by inserting a string into a specified position in another string. This method uses a zero-based index. The following example inserts a string into the fifth index position of MyString and creates a new string with this value. C# Copy.


2 Answers

There is no overload for String.Split which takes just a string, instead use the next closest match:

List<string> s = new List<string>(     sss.Split(new string[] { "125" }, StringSplitOptions.None)); 
like image 83
user7116 Avatar answered Sep 24 '22 05:09

user7116


This confused me for a long time. Finally I realised that I had used double instead of single quotes. In other words, I had x.Split(",") rather than x.Split(',').

I changed to single quotes and it worked for me.

like image 35
Tom Roth Avatar answered Sep 25 '22 05:09

Tom Roth