Split string requires array declaration

前端 未结 2 897
孤独总比滥情好
孤独总比滥情好 2021-01-29 13:06

string myStr = \"part1#part2\";

To split this simple string Split() method expect array with arguments to be passed. Really? Why I can\'t just specify

相关标签:
2条回答
  • 2021-01-29 13:13

    You can. String.Split takes a param argument, which allows for variable number of arguments.

    The following works as expected

    var text = "a,a,a";
    var parts = text.Split(',');
    
    0 讨论(0)
  • 2021-01-29 13:33

    Another Way,


    Some more on Brown Fox

    string data = "THE1QUICK1BROWN1FOX";
    
    return data.Split(new string[] { "1" }, StringSplitOptions.None);
    

    Some more from http://stackoverflow.com

    var str = "google.com 420 AM 3 May 12";
    var domain = str.Split(' ')[0];           // google.com
    var tld = domain.Substring(domain.IndexOf('.')) // .com
    

    Ref:http://msdn.microsoft.com/en-us/library/b873y76a.aspx

    i am late but with ,

    A Simple Tutor


    The simplest syntax of the Split() method accepts a character array as it's only parameter, listing the characters to use in determining where splitting of the string should occur. It returns an array of strings, with each element of the array corresponding to the value between the specified delimiter(s). The line below is from the first split operation in the listing:

        string[] year = commaDelimited.Split(new char[] {','});
    

    In a similar manner, elements of an array may be combined into a delimited string by using the Join() method. The simplest overload of the Join() method accepts two parameters: a string, which separates each array element, and the array of elements to be combined. The Join() method is static, requiring the String type identifier, rather than a string instance, to implement the command. The following line from the listing creates a string with all year elements in sequence, separated by colons:

        string colonDelimeted = String.Join(":", year);
    

    Overloads


    Those were the simple implementations of these methods, and probably the most likely to be used. Now lets take a peek at a couple of their overloads to see how to implement more specialized behavior.

    The Split() method has an overload with a second parameter, specifying the number of separations to implement. The next line will separate the commaDelimited string into three array elements:

        string[] quarter = commaDelimited.Split(new Char[] {','}, 3);
    

    At first thought, one may think that the three array elements could be Jan, Feb, and Mar, but this is not so. The first array element is Jan, the second is Feb, and the last is the rest of the string. To see for yourself, here's the output string with each array element separated by a space:

    Jan Feb Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
    

    The Join() method has an overload that allows you to extract a subset of an array. The first two parameters are the same as previously described, and the third and fourth parameters specify the position in the array to begin reading and the number of elements to read, respectively. The following line from the listing creates a string with a forward slash between the sixth through eighth elements of the year array:

        string thirdQuarter = String.Join("/", year, 6, 3);
    
    0 讨论(0)
提交回复
热议问题