Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 18-Jan-11 23:40pm
v2

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


or if you want it as int[]:
C#
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]);
}
 
Share this answer
 
v2
Comments
#realJSOP 19-Jan-11 5:50am    
This is the first instance of the correct answer.
Hiren solanki 19-Jan-11 5:57am    
very good answer among all.
honey4bee 19-Jan-11 6:52am    
thank u,,, goog answer this
thatraja 19-Jan-11 9:58am    
Awesome answer Jens
There's method for that :

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


to int array :

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


Cheers
 
Share this answer
 
Dim str as string="4,5,6"

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


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

VB
For i As Integer = 0 To arr.Length - 1
            ListBox1.Items.Add(arr(i).ToString)
        Next
 
Share this answer
 
Also check string functions[^]
 
Share this answer
 
Have a look at String.Split[^] method. Once you have the substrings you may easily convert them to numbers.
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900