根据多个字符分隔符分割字符串 [英] splitting a string based on multiple char delimiters

查看:116
本文介绍了根据多个字符分隔符分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想根据和\n

输出数组应为

4
6
8
9
4

编辑:

现在我正在从控制台读取字符串,当我在console中输入如上的字符串时,在后面的代码中我将得到为 4,6,8 \\n9,4 。现在,我想使用,和 \\n 进行拆分。如何更改表达方式?

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 ?

推荐答案

使用 string.Split(char [])

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

编辑

如果有不必要的空白物品,请尝试以下方法。 String.Split方法(String [],StringSplitOptions)

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

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

EDIT2

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

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

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

这篇关于根据多个字符分隔符分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆