Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to figure this out to no end. If someone can help, it would be appreciated. I want to split this string into an array and then assign each variable to a label. I have been able to get the string to split in a few different way But when it come time to assign each line there where I get totally lost. ie string x = (G\F\3\H\Y) I can get the following returned in an array
G
F
3
H
Y

I want to pull item based on the position in the string. lenght[1] lenght[2] and so on. The string itself changes but the position remain the same and total lenght of sting remain the same.

Label1.Text = lenght[1](G) 
label2.Text = lenght[2](F) 
Label3.Text = Lenght[3](3) and so on.

I am not explaining this very well. Hopefully someone will understand what I am trying to do.
Thank you ie code below

string info = (G\F\3\6\H\Y)
string info2 = info.Replace(@"\", ":");
               string[] info3 = new string[25];
               object[] list = new object[info3.Length];
               char[] Splitter = { ':' };
               string[] arrinfox = info2.Split(Splitter);
               for (int i = 0; i < arrinfox.Length; i++)
               {
                   //here is where I get lost
               }
Posted
Updated 10-Oct-10 15:46pm
v2

So you are having trouble referencing the label by its ID? Perhaps this will help:
C#
((Label)FindControl("Label" + (i + 1).ToString())).Text = arrinfox[i];
 
Share this answer
 
You have several things incorrect with your code, honestly it is horrible. This what it should be

string info = @"G\F\3\6\H\Y";

// Initialize list of labels appropriately
List<label> labels = new List<label>();

string[] split = info.Split(new char[] {'\\'});

for(int x = 0; x < split.Length; x++)
{
    // Assumes number of labels equals number of strings
    labels[x].Text = split[x];
}</label></label>
 
Share this answer
 
Comments
Robert Adamo 10-Oct-10 22:09pm    
Thanks, I tried something similar but the string org length sometime changes. So i need to create the labels dynamically or to simplify match string length then assign it to different string name. str1 = G, str2 = F etc and but the string need to match the length of original string which might vary.
[no name] 11-Oct-10 8:00am    
Your response makes no sense to me. Can you clarify what you are asking for?

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