如何将String拆分为多个值?

时间:2021-08-14 21:36:41

How do you split a string?

你怎么分裂一个字符串?

Lets say i have a string "dog, cat, mouse,bird"

可以说我有一个字符串“狗,猫,老鼠,鸟”

My actual goal is to insert each of those animals into a listBox, so they would become items in a list box.

我的实际目标是将每个动物插入一个列表框,这样它们就会成为列表框中的项目。

but i think i get the idea on how to insert those items if i know how to split the string. or does anyone know a better way to do this?

但我想如果我知道如何拆分字符串,我会想到如何插入这些项目。还是有人知道更好的方法吗?

im using asp c#

即时通讯使用asp c#

5 个解决方案

#1


7  

    string[] tokens = text.Split(',');

    for (int i = 0; i < tokens.Length; i++)
    {
          yourListBox.Add(new ListItem(token[i], token[i]));
    }

#2


4  

Have you tried String.Split? You may need some post-processing to remove whitespace if you want "a, b, c" to end up as {"a", "b", "c"} but "a b, c" to end up as {"a b", "c"}.

你试过String.Split吗?如果你想要“a,b,c”结束为{“a”,“b”,“c”}但是“ab,c”结束为{“ab”,你可能需要一些后处理来删除空白“, “C”}。

For instance:

private readonly char[] Delimiters = new char[]{','};

private static string[] SplitAndTrim(string input)
{
    string[] tokens = input.Split(Delimiters,
                                  StringSplitOptions.RemoveEmptyEntries);

    // Remove leading and trailing whitespace
    for (int i=0; i < tokens.Length; i++)
    {
        tokens[i] = tokens[i].Trim();
    }
    return tokens;
}

#3


4  

Needless Linq version;

不用Linq版;

from s in str.Split(',')
where !String.IsNullOrEmpty(s.Trim())
select s.Trim();

#4


2  

Or simply:

targetListBox.Items.AddRange(inputString.Split(','));

Or this to ensure the strings are trimmed:

或者这是为了确保修剪字符串:

targetListBox.Items.AddRange((from each in inputString.Split(',')
    select each.Trim()).ToArray<string>());

Oops! As comments point out, missed that it was ASP.NET, so can't initialise from string array - need to do it like this:

哎呀!正如评论指出的那样,错过了它是ASP.NET,因此无法从字符串数组初始化 - 需要这样做:

var items = (from each in inputString.Split(',')
    select each.Trim()).ToArray<string>();

foreach (var currentItem in items)
{
    targetListBox.Items.Add(new ListItem(currentItem));
}

#5


1  

It gives you a string array by strVar.Split

它通过strVar.Split为您提供了一个字符串数组

"dog, cat, mouse,bird".Split(new[] { ',' });

#1


7  

    string[] tokens = text.Split(',');

    for (int i = 0; i < tokens.Length; i++)
    {
          yourListBox.Add(new ListItem(token[i], token[i]));
    }

#2


4  

Have you tried String.Split? You may need some post-processing to remove whitespace if you want "a, b, c" to end up as {"a", "b", "c"} but "a b, c" to end up as {"a b", "c"}.

你试过String.Split吗?如果你想要“a,b,c”结束为{“a”,“b”,“c”}但是“ab,c”结束为{“ab”,你可能需要一些后处理来删除空白“, “C”}。

For instance:

private readonly char[] Delimiters = new char[]{','};

private static string[] SplitAndTrim(string input)
{
    string[] tokens = input.Split(Delimiters,
                                  StringSplitOptions.RemoveEmptyEntries);

    // Remove leading and trailing whitespace
    for (int i=0; i < tokens.Length; i++)
    {
        tokens[i] = tokens[i].Trim();
    }
    return tokens;
}

#3


4  

Needless Linq version;

不用Linq版;

from s in str.Split(',')
where !String.IsNullOrEmpty(s.Trim())
select s.Trim();

#4


2  

Or simply:

targetListBox.Items.AddRange(inputString.Split(','));

Or this to ensure the strings are trimmed:

或者这是为了确保修剪字符串:

targetListBox.Items.AddRange((from each in inputString.Split(',')
    select each.Trim()).ToArray<string>());

Oops! As comments point out, missed that it was ASP.NET, so can't initialise from string array - need to do it like this:

哎呀!正如评论指出的那样,错过了它是ASP.NET,因此无法从字符串数组初始化 - 需要这样做:

var items = (from each in inputString.Split(',')
    select each.Trim()).ToArray<string>();

foreach (var currentItem in items)
{
    targetListBox.Items.Add(new ListItem(currentItem));
}

#5


1  

It gives you a string array by strVar.Split

它通过strVar.Split为您提供了一个字符串数组

"dog, cat, mouse,bird".Split(new[] { ',' });