基于多个字符分隔符拆分字符串 [英] splitting a string based on multiple char delimiters

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

问题描述

我有一个字符串 "4,6,8 9,4"

I have a string "4,6,8 9,4"

我想根据 , 和

输出数组应该是

4
6
8
9
4

现在我正在从控制台读取字符串,当我在控制台中输入上述字符串时,在后面的代码中我得到 "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
9,4";
string [] split = strings .Split(new Char [] {',' , '
' });

编辑

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

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

string [] split = strings .Split(new Char [] {',' , '
' }, 
                                 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[] { ',', '\', '
' },
                                 StringSplitOptions.RemoveEmptyEntries);

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

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