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

前端 未结 7 1837

I am using the following code to split a string:

string sss=\"125asdasdlkmlkdfknkldj125kdjfngdkjfndkg125ksndkfjdks125\";

List s = new List<         


        
相关标签:
7条回答
  • 2020-12-11 01:22

    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'}); 
    
    0 讨论(0)
  • 2020-12-11 01:27

    Try this:

    //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 讨论(0)
  • 2020-12-11 01:31

    this error/issue is not present in .NET Core 3.1 but does appear in .NET 4.7.2. This error came up while trying to use C# fiddle (https://dotnetfiddle.net/) to test something and was not getting the same message in my project. The issue was that my project is using .NET Core compiler and the fiddle was not. Once that was updated in the fiddle, the error cleared.

    0 讨论(0)
  • 2020-12-11 01:42

    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.

    0 讨论(0)
  • 2020-12-11 01:43

    You can just create a 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()));
    

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

    0 讨论(0)
  • 2020-12-11 01:44

    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.

    0 讨论(0)
提交回复
热议问题