C# How to split string based on 2 parameters?

后端 未结 2 752
借酒劲吻你
借酒劲吻你 2021-01-29 07:49

I have three string called This_is string1 and This is string2 There_is string3

How to split these 3 strings after \"This_\", \"T

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

    The answer is simple, you can use the String.Split method, specifying the multiple delimiters (in your case the underscore and whitespace):

    str.Split(new char[]{'_',' '})
    

    LinQPad result:

    enter image description here

    If you want to split only the first part, you can use the 2nd overload of String.Split:

    str.Split(new char[]{'_',' '}, 2);
    

    and this is the result in LinQPad:

    enter image description here

    0 讨论(0)
  • 2021-01-29 08:45

    So you want to split only the first part? You can use the overload of String.Split that allows to specify the count and multiple delimiters:

    str.Split(new[]{' ', '_'}, 2);
    

    So on the first string you get: "This" + "is string1", similar on the others.

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