Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi All,

I need to take a value in a string (from a rich text box) I have managed to split up the text into substrings with the the split property of strings.
I need to place these in an array to allow them to read seperatly and added to other strings and on. I have the following
foreach (string subString in ValueCommand2.Split('\n'))
{

    Command2[g] = subString;
    g++;
    // MessageBox.Show(Command2);
}

That is the correct wat of splitting up the string right?
The problem is now declairing Command2 as an array of strings
I have
string Command2[] = new string array[5];

This is wrong isn't it?
Glenn
Posted

Split[^] already gives you an array of Strings, so all you have to do is this:

C#
String ValueCommand2 = "/* some lines of text belong in here */";
String[] Command2 = ValueCommand2.Split(new char[] { '\n' });


You can get the size of the array by using the Array's Length property.

Regards,

—MRB
 
Share this answer
 
v2
Comments
glennPattonWork3 9-Dec-11 6:10am    
Thanks didn't think of that!
Glenn
Abhinav S 9-Dec-11 6:21am    
5!
[no name] 21-Oct-12 14:16pm    
*****
I use "List<string> stringlist = new List<string>()" This enables me to use stringlist.Add("string to add"); and you can use stringlist.sort(). and you don't have to declare a specific size when you create it...

stringlist = ValueCommand2.Split('\n').ToList() //bam
 
Share this answer
 
v4
Solved it!
C#
string Command2[] =  new string[5];
string[] Command2 =  new string[5];

Note to Self Look at your code before you shout for help!
 
Share this answer
 
v3
Comments
glennPattonWork3 17-May-12 11:36am    
Thanks for that, must admit I had forgotten I asked this question. Scarily similar to a problem I was (am having) at the moment with some somewhere to interface to a board more or less like that one!

Glenn

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