如何分离字符串并保存在ASP.NET中的数组中[英] How to separate string and save in the array in asp.net

本文是小编为大家收集整理的关于如何分离字符串并保存在ASP.NET中的数组中的处理方法,想解了如何分离字符串并保存在ASP.NET中的数组中的问题怎么解决?如何分离字符串并保存在ASP.NET中的数组中问题的解决办法?如何分离字符串并保存在ASP.NET中的数组中问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我有DB中的列,将字符串保存为1,2,3,4,5 我想将字符串分离为1 2 3 4 5并将每个数字保存在数组中,例如,数组应该像int [] numbers,int [0] = 1,int [1] = 2 ..... .

推荐答案

看起来您想要:

int[] array = text.Split(',')
                  .Select(x => int.Parse(x, CultureInfo.InvariantCulture))
                  .ToArray();

显然,如果任何拆分结果都不是可拆卸整数.

(您也可能想使用List<int>而不是数组,作为通常更灵活的收集类型.)

编辑:简短但完整的演示程序:

using System;
using System.Globalization;
using System.Linq;

public class Test
{
    static void Main()
    {
        string eventIds = "1,2,3";
        int[] array =
            eventIds.Split(',')
                    .Select(x => int.Parse(x, CultureInfo.InvariantCulture))
                    .ToArray();
        foreach (int id in array)
        {
            Console.WriteLine(id);
        }
    }
}

其他推荐答案

这是代码

Dim str as String = "1,2,3,4,5"

dim objstr() as String = str.split(",")

在这里拆分返回阵列.

其他推荐答案

这是John的出色代码段的"更安全"版本,它将通过将它们添加为:

来处理无法解析为int的值
int temp;
int[] array = text.Split(',')
    .Select(x => int.TryParse(x, out temp) ? temp : 0)
    .ToArray();

如果您想完全安全并丢弃无法转换为整数的任何值,那么您可以使用这样的函数:

private static int[] ConvertTextToIntArray(string text)
{
    var integerList = new List<int>();
    var values = text.Split(',');
    int temp;

    foreach (var value in values)
    {
        if (int.TryParse(value, out temp))
        {
            integerList.Add(temp);
        }
    }

    return integerList.ToArray();
}

本文地址:https://www.itbaoku.cn/post/2352429.html

问题描述

I have got column in db which saves the string as 1,2,3,4,5 I want to separate the string to 1 2 3 4 5 and save each number in the array , for instance the array should be like int [] numbers , int[0] = 1 , int[1] = 2 ......

推荐答案

Looks like you want:

int[] array = text.Split(',')
                  .Select(x => int.Parse(x, CultureInfo.InvariantCulture))
                  .ToArray();

Obviously this will go bang if any of the split results is not a parsable integer.

(You may also want to use a List<int> instead of an array, as a generally more flexible collection type.)

EDIT: Short but complete demo program:

using System;
using System.Globalization;
using System.Linq;

public class Test
{
    static void Main()
    {
        string eventIds = "1,2,3";
        int[] array =
            eventIds.Split(',')
                    .Select(x => int.Parse(x, CultureInfo.InvariantCulture))
                    .ToArray();
        foreach (int id in array)
        {
            Console.WriteLine(id);
        }
    }
}

其他推荐答案

here is code

Dim str as String = "1,2,3,4,5"

dim objstr() as String = str.split(",")

here split return array.

其他推荐答案

Here's a "safer" version of John's excellent code snippet that will deal with values that cannot be parsed as int by adding them as 0:

int temp;
int[] array = text.Split(',')
    .Select(x => int.TryParse(x, out temp) ? temp : 0)
    .ToArray();

If you want to be totally safe and discard any values that cannot be converted to an integer then you could use a function like this:

private static int[] ConvertTextToIntArray(string text)
{
    var integerList = new List<int>();
    var values = text.Split(',');
    int temp;

    foreach (var value in values)
    {
        if (int.TryParse(value, out temp))
        {
            integerList.Add(temp);
        }
    }

    return integerList.ToArray();
}
查看更多