问题描述
C#的新手(从Delphi迁移),我不知道如何将一个
分隔的字符串转换为字符串数组。
输入:字符串看起来像 - 01-85-78-15-Q11
输出:带元素的数组 - 01,85,78,15等......
是否有某种简单的衬垫可以做到这一点?很难搜索
帮助我执行此操作的帮助文件。
提前致谢。
New to C# (migrating from Delphi) and I''m not sure how to get a
delimited string into an array of string.
input: string looks like - 01-85-78-15-Q11
output: an array with elements - 01,85,78,15 etc...
Is there some sort of easy one liner that does this? Hard to search
the help files for anything that would assist me in doing this.
Thanks in advance.
推荐答案
您有两种选择:
1)简单 - 使用string.Split。
string input =" 01-85-78" ;;
string [] output = input.Split('' - '');
2)简单,更灵活,但也许是矫枉过正 - 使用正则表达式
string input =" 01-85-78" ;;
string [] output = Regex.Split(input," - ");
如果你遇到更复杂的
分割要求,我只会使用正则表达式选项。
-mdb
You have two options:
1) Easy - use string.Split.
string input = "01-85-78";
string[] output = input.Split(''-'');
2) Easy, more flexible, but perhaps overkill - use Regex
string input = "01-85-78";
string[] output = Regex.Split(input, "-");
I would only use the Regex option if you get into more complicated
splitting requirements.
-mdb
这篇关于分隔字符串到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!