根据多个字符分隔符分割字符串

发布于 2024-12-07 11:23:45 字数 264 浏览 0 评论 0原文

我有一个字符串“4,6,8\n9,4”,

我想根据它进行分割,并且 \n

输出数组应该是

4
6
8
9
4

编辑:

现在,当我输入时,我正在从控制台读取字符串控制台中如上所述的字符串,在后面的代码中我得到 "4,6,8\\n9,4" 。现在我想使用 "," 和 "\\n" 进行分割。我怎样才能改变表达方式?

I have a string "4,6,8\n9,4"

I want to split this based on ,and \n

Output array should be

4
6
8
9
4

Edit :

Now i am reading string from console , when i enter a string as above in console , in the code behind i get as "4,6,8\\n9,4" . Now that i want to split using "," and "\\n" . How can i change the expression ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

帥小哥 2024-12-14 11:23:45

使用 string.Split(char [])

string strings = "4,6,8\n9,4";
string [] split = strings .Split(new Char [] {',' , '\n' });

编辑

如果您收到任何不必要的空物品,请尝试以下操作 String.Split 方法(String[]、StringSplitOptions)

string [] split = strings .Split(new Char [] {',' , '\n' }, 
                                 StringSplitOptions.RemoveEmptyEntries);

EDIT2

这适用于您更新的问题。将所有必要的分割字符添加到 char [] 中。

string [] split = strings.Split(new Char[] { ',', '\\', '\n' },
                                 StringSplitOptions.RemoveEmptyEntries);

Use string.Split(char [])

string strings = "4,6,8\n9,4";
string [] split = strings .Split(new Char [] {',' , '\n' });

EDIT

Try following if you get any unnecessary empty items. String.Split Method (String[], StringSplitOptions)

string [] split = strings .Split(new Char [] {',' , '\n' }, 
                                 StringSplitOptions.RemoveEmptyEntries);

EDIT2

This works for your updated question. Add all the necessary split characters to the char [].

string [] split = strings.Split(new Char[] { ',', '\\', '\n' },
                                 StringSplitOptions.RemoveEmptyEntries);
乙白 2024-12-14 11:23:45

另一种选择是使用 Regex.Split。当分割序列更复杂时,这非常有用。例如,如果空格也可以是分割定界符的一部分,例如:

"4,6,8 , 9\\n\\n4"

那么:

using System.Text.RegularExpressions;
var i = "4,6,8 , 9\n\n4";
var o = Regex.Split(i, @"[,\s\n]+");
// now o is:
// new string[] { "4", "6", "8", "9" }

请注意,使用的正则表达式“更容易接受” - 它忽略了 \n 之间的空“空格”,并且会接受“4 6 8 9 4” ” 同样 - 所以上面要表明一点:给猫剥皮的方法不止一种。

快乐编码。

Another option is to use Regex.Split. This is useful when the split sequences are more complex. For instance if spaces can also be part of the split delimiters such as:

"4,6,8 , 9\\n\\n4"

Then:

using System.Text.RegularExpressions;
var i = "4,6,8 , 9\n\n4";
var o = Regex.Split(i, @"[,\s\n]+");
// now o is:
// new string[] { "4", "6", "8", "9" }

Note that the regular expression used is "more accepting" - it ignored the empty "space" between the \n's and it would accept "4 6 8 9 4" just the same - so the above to to show a point: there is more than one way to skin a cat.

Happy coding.

靑春怀旧 2024-12-14 11:23:45
var s = "4,6,8\n9,4";
var split = s.Split(new char[]{',', '\n'});

但这一定是一个骗局...

编辑:解决评论。

此代码:

static void Main(string[] args)
{
    var s = "4,6,8\n9,4";

    foreach (var a in s.Split(new char[] { ',', '\n' }))
        System.Diagnostics.Debug.WriteLine(a);
}

输出:

4
6
8
9
4

编辑:从控制台读取输入是不同的。手动输入时 \n 不同。

static void Main(string[] args)
{
    var s = "4,6,8\\n9,4";

    foreach (var a in s.Split(new string[] { ",", "\\n" }, StringSplitOptions.RemoveEmptyEntries))
        System.Diagnostics.Debug.WriteLine(a);
}
var s = "4,6,8\n9,4";
var split = s.Split(new char[]{',', '\n'});

But this has to be a dupe...

EDIT: Addressing the comment.

This code:

static void Main(string[] args)
{
    var s = "4,6,8\n9,4";

    foreach (var a in s.Split(new char[] { ',', '\n' }))
        System.Diagnostics.Debug.WriteLine(a);
}

Outputs this:

4
6
8
9
4

EDIT: Reading input from the console is different. \n is different when entered manually.

static void Main(string[] args)
{
    var s = "4,6,8\\n9,4";

    foreach (var a in s.Split(new string[] { ",", "\\n" }, StringSplitOptions.RemoveEmptyEntries))
        System.Diagnostics.Debug.WriteLine(a);
}
世界等同你 2024-12-14 11:23:45
string tosplit = "4,6,8\n9,4";
var split = tosplit.Split(new Char [] {',', '\n' });

以防万一您没有正确打印/查看它:

split.ToList().ForEach(Console.WriteLine);
string tosplit = "4,6,8\n9,4";
var split = tosplit.Split(new Char [] {',', '\n' });

Just in case you are not printing / seeing it properly:

split.ToList().ForEach(Console.WriteLine);
琴流音 2024-12-14 11:23:45

您可以执行 string.Replace('\n',',') 后跟 string.split(',') 吗?

Can you do a string.Replace('\n',',') followed by the string.split(',') ?

失去的东西太少 2024-12-14 11:23:45
// input string
string input = "[email protected];[email protected],[email protected];";

// each character in this string will split it
string splitBy = ",;";

// do the split, remove any blank results
string[] result = input.Split(splitBy.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

输出是一个字符串数组:

[email protected]
[email protected]
[email protected]
// input string
string input = "[email protected];[email protected],[email protected];";

// each character in this string will split it
string splitBy = ",;";

// do the split, remove any blank results
string[] result = input.Split(splitBy.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

output is an array of strings:

[email protected]
[email protected]
[email protected]
爱人如己 2024-12-14 11:23:45

对于每种情况

    string s = "";
    s.Split(new string[] { ",", "DEL", ";"}, StringSplitOptions.None);

for every cases

    string s = "";
    s.Split(new string[] { ",", "DEL", ";"}, StringSplitOptions.None);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文