如何使用C#将字符串拆分一次

时间:2021-07-07 22:38:39

Example : a - b - c must be split as a and b - c, instead of 3 substrings

示例:a - b - c必须拆分为a和b - c,而不是3个子串

8 个解决方案

#1


Specify the maximum number of items that you want:

指定所需的最大项目数:

string[] splitted = text.Split(new string[]{" - "}, 2, StringSplitOptions.None);

#2


string s = "a - b - c";
string[] parts = s.Split(new char[] { '-' }, 2);
// note, you'll still need to trim off any whitespace

#3


"a-b-c".Split( new char[] { '-' }, 2 );

#4


You could use indexOf() to find the first instance of the character you want to split with, then substring() to get the two aspects. For example...

您可以使用indexOf()来查找要分割的字符的第一个实例,然后使用substring()来获取这两个方面。例如...

int pos = myString.IndexOf('-');
string first = myString.Substring(0, pos);
string second = myString.Substring(pos);

This is a rough example - you'll need to play with it if you don't want the separator character in there - but you should get the idea from this.

这是一个粗略的例子 - 如果你不想在那里使用分隔符,你需要使用它 - 但你应该从中得到这个想法。

#5


string[] splitted = "a - b - c".Split(new char[]{' ', '-'}, 2, StringSplitOptions.RemoveEmptyEntries);

#6


#7


var str = "a-b-c";
int splitPos = str.IndexOf('-');
string[] split = { str.Remove(splitPos), str.Substring(splitPos + 1) };

#8


I have joined late and many of above answers are matched with my following words:

我加入了很晚,上面的许多答案与我的以下词语相符:

string has its own

字符串有自己的

Split

You can use the same to find the solution of your problem, following is the example as per your issue:

您可以使用相同的方法来查找问题的解决方案,以下是您的问题的示例:

using System;

public class Program
{
    public static void Main()
    {
        var PrimaryString = "a - b - c";
        var strPrimary  = PrimaryString.Split( new char[] { '-' }, 2 );
        Console.WriteLine("First:{0}, Second:{1}",strPrimary[0],strPrimary[1]);

    }
}

Output:
First:a , Second: b - c

#1


Specify the maximum number of items that you want:

指定所需的最大项目数:

string[] splitted = text.Split(new string[]{" - "}, 2, StringSplitOptions.None);

#2


string s = "a - b - c";
string[] parts = s.Split(new char[] { '-' }, 2);
// note, you'll still need to trim off any whitespace

#3


"a-b-c".Split( new char[] { '-' }, 2 );

#4


You could use indexOf() to find the first instance of the character you want to split with, then substring() to get the two aspects. For example...

您可以使用indexOf()来查找要分割的字符的第一个实例,然后使用substring()来获取这两个方面。例如...

int pos = myString.IndexOf('-');
string first = myString.Substring(0, pos);
string second = myString.Substring(pos);

This is a rough example - you'll need to play with it if you don't want the separator character in there - but you should get the idea from this.

这是一个粗略的例子 - 如果你不想在那里使用分隔符,你需要使用它 - 但你应该从中得到这个想法。

#5


string[] splitted = "a - b - c".Split(new char[]{' ', '-'}, 2, StringSplitOptions.RemoveEmptyEntries);

#6


#7


var str = "a-b-c";
int splitPos = str.IndexOf('-');
string[] split = { str.Remove(splitPos), str.Substring(splitPos + 1) };

#8


I have joined late and many of above answers are matched with my following words:

我加入了很晚,上面的许多答案与我的以下词语相符:

string has its own

字符串有自己的

Split

You can use the same to find the solution of your problem, following is the example as per your issue:

您可以使用相同的方法来查找问题的解决方案,以下是您的问题的示例:

using System;

public class Program
{
    public static void Main()
    {
        var PrimaryString = "a - b - c";
        var strPrimary  = PrimaryString.Split( new char[] { '-' }, 2 );
        Console.WriteLine("First:{0}, Second:{1}",strPrimary[0],strPrimary[1]);

    }
}

Output:
First:a , Second: b - c