将字符串传递给数组 [英] Passing string to an array

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

问题描述

我有一个名为str ="4,5,6"的字符串,
我想将这些值分配给数组
即数组中的第一个元素为4,
第二个是5,等等,
在这里我给出了3个值,但实际上我不知道它在字符串中有多少个值,
是否可以将该字符串分配给数组,
如果您知道可以帮助我,
在我需要的应用程序中.

I have a string called str="4,5,6",
i want to assign those values to an array
i.e in the array the first element is 4,
second is 5, etc,,
here i gave 3 values but actually i dont know how many values it have in string,
is it possible to assign that string to an array,
if u know help me,
in my app that is need.

推荐答案

string str = "4,5,6";
string[] strings = str.Split(',');



或者如果您希望将其作为int []:



or if you want it as int[]:

string str = "4,5,6";
string[] strings = str.Split(',');
int[] ints = new int[strings.Length];
for(int i = 0; i < strings.Length; i++)
{
  Int32.TryParse(strings[i], out ints[i]);
}


有相应的方法:

There''s method for that :

string[] s = "4,5,6".Split('','');



到int数组:



to int array :

int[] ints = s.Select(x => int.Parse(x)).ToArray();



欢呼



Cheers


Dim str as string="4,5,6"


Dim arr() As String = str.ToString.Split(",")



现在,使用for循环,您可以将此数据添加到任何数据表,数据集中或根据您的要求.

例如将此数据添加到列表框



Now, using for loop you can add this data in any datatable, dataset or as per your requirement.

For Eg. to add this data in Listbox

For i As Integer = 0 To arr.Length - 1
            ListBox1.Items.Add(arr(i).ToString)
        Next


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

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