分隔字符串到数组 [英] Delimited String to Array

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

问题描述

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.

推荐答案

" Kyro" < ky ***** @ gmail.comwrote in news:1155322984.111975.115810

@ i3g2000cwc.googlegroups.com:
"Kyro" <ky*****@gmail.comwrote in news:1155322984.111975.115810
@i3g2000cwc.googlegroups.com:

新到C#(从Delphi迁移),我不知道如何将一个

分隔的字符串放入一个字符串数组中。


输入:string看起来喜欢 - 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.



您有两种选择:


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


Hello Kyro,


见String.Split / String.Join方法


KNew到C#(从Delphi迁移),我不知道如何获得一个

Kdelimited字符串到一个字符串数组。

K>

Kinput:字符串看起来像 - 01-85-78-15-Q11

Koutput:一个数组与元素 - 01,85,78,15等...

KIs有一些简单的一个衬里tha这样做?很难搜索

K帮助文件可以帮助我做任何事情。

K>

KThanks提前。

K>

---

WBR,

Michael Nemtsev ::博客: http://spaces.msn.com/laflour


"有时一个人仍然忠于一个事业,只因为它的对手不会因为他们不会平淡无味。 (c)Friedrich Nietzsche
Hello Kyro,

See String.Split/String.Join methods

KNew to C# (migrating from Delphi) and I''m not sure how to get a
Kdelimited string into an array of string.
K>
Kinput: string looks like - 01-85-78-15-Q11
Koutput: an array with elements - 01,85,78,15 etc...
KIs there some sort of easy one liner that does this? Hard to search
Kthe help files for anything that would assist me in doing this.
K>
KThanks in advance.
K>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


你好Kyro,


BTW,你甚至可以使用String.Replace


KNew到C#(从Delphi迁移),我不知道怎么把一个

Kdelimited字符串变成一个字符串数组。

K> ;

Kinput:字符串看起来像 - 01-85-78-15-Q11

Koutput:包含元素的数组 - 01,85,78,15等...

KIs有哪种简单的衬垫可以做到这一点?很难搜索

K帮助文件可以帮助我做任何事情。

K>

KThanks提前。

K>

---

WBR,

Michael Nemtsev ::博客: http://spaces.msn.com/laflour


"有时一个人仍然忠于一个事业,只因为它的对手不会因为他们不会平淡无味。 (c)Friedrich Nietzsche
Hello Kyro,

BTW, u even could use String.Replace

KNew to C# (migrating from Delphi) and I''m not sure how to get a
Kdelimited string into an array of string.
K>
Kinput: string looks like - 01-85-78-15-Q11
Koutput: an array with elements - 01,85,78,15 etc...
KIs there some sort of easy one liner that does this? Hard to search
Kthe help files for anything that would assist me in doing this.
K>
KThanks in advance.
K>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


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

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