如何断线在每一个逗号? [英] How to break a string at each comma?

查看:127
本文介绍了如何断线在每一个逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我手边有一个问题,我似乎无法弄清楚,我有一个字符串(C#)这看起来是这样的:

Hi guys I have a problem at hand that I can't seem to figure out, I have a string (C#) which looks like this:

string tags = "cars, motor, wheels, parts, windshield";

我要打破这个字符串在每一个逗号,并得到每个单词分配给一个新的字符串本身,如:

I need to break this string at every comma and get each word assign to a new string by itself like:

string individual_tag = "car";

我知道我必须做一些循环在这里,但我真的不知道如何处理这一点,任何帮助将是非常美联社preciate吧。

I know I have to do some kind of loop here but I'm not really sure how to approach this, any help will be really appreciate it.

推荐答案

您可以使用的 String.Split 方法

Split Method (Char[])
Split Method (Char[], StringSplitOptions)
Split Method (String[], StringSplitOptions)

让我们尝试第二个选项: 我给和空间,在其中每个人物出现输入字符串拆分字符,然后将被分割,但可以在结果空字符串。我们可以使用删除 StringSplitOptions.RemoveEmptyEntries 参数。

let's try second option: I'm giving , and space as split chars then on each those character occurrence input string will be split, but there can be empty strings in the results. we can remove them using StringSplitOptions.RemoveEmptyEntries parameter.

string[] tagArray = tags.Split(new char[]{',', ' '},
                               StringSplitOptions.RemoveEmptyEntries);

 string[] tagArray = s.Split(", ".ToCharArray(), 
                               StringSplitOptions.RemoveEmptyEntries);

您可以通过访问每个标签:

you can access each tag by:

foreach (var t in tagArray )
{
    lblTags.Text = lblTags.Text + " " + t; // update lable with tag values 
    //System.Diagnostics.Debug.WriteLine(t); // this result can be see on your VS out put window 
}

这篇关于如何断线在每一个逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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