0

Let's start with that i have a txtProbe(textbox) and there i have 12-34-56-78-90. I want to parse them in different labels or textboxes ... For now just in one textbox - txtParse. I tried with that code - I'm removing the "-" and then tried to display them but nothing happens:

{ char[] delemiterChars = { '-' }; string text = txtProbe.Text; string[] words = text.Split(delemiterChars); txtParse.Text = text; foreach (string s in words) { txtParse.Text = s; } } 

EDIT: I want to set the received information in different labels:
12-label1
34-label2
56-label3
78-label4
90-label5

4
  • 3
    It doesn't look like nothing should happen; it looks like the textbox should have the last value, since you overwrite the value of the textbox every time. Commented Oct 30, 2012 at 14:56
  • is this web forms or windows forms? Commented Oct 30, 2012 at 15:01
  • @NikolaObretenov Will there always be five labels or more? Are you creating them dynamically? No heart feelings but with this much people involved in helping you if you could post your questions better you would certainly get appropriate answers. Commented Oct 30, 2012 at 15:13
  • It will be of that how many --**.... i have ... for now they are 5 Commented Oct 30, 2012 at 15:23

7 Answers 7

2

You could just use String.Replace:

txtParse.Text = txtProbe.Text.Replace("-", " "); 
Sign up to request clarification or add additional context in comments.

7 Comments

I think that's missing the point. He wants each part in a different label or text box.
@MarkByers That should be easy enough to change as needed though.
Yes, obviously it's very easy, but the OP doesn't know how to do it and that's what his (poorly explained) question is trying to ask.
Yes i want to set all the results in different labels 12- labelx1 34- labelx2 56 - labelx3
@NikolaObretenov: The confusing thing is the way you have written your question. You need to make it much more clear. Pretend you are talking to children that only understand simple sentences and see if you can explain to them what you are trying to do. Your comments are very clear. However your question is not at all clear. And it's the question that is the most important. Remember that not everyone is going to read all the comments. You need to get your point across in the question.
|
2

The following 'll do the trick more "semantically":

var parsed = text.Replace("-", " "); 

You might be changing the value too early in the Page Life Cycle (in the case of Web Forms), with regards to why you're not seeing the parsed value in the server control.

Comments

1

For your specific sample it seems that you could just replace '-' by ' '.

txtParse.Text = txtProbe.Text.Replace('-', ' '); 

But in order to join an array of strings using a white space separator, you could use this

txtParse.Text = string.Join(" ", words); 

Your logic is not appropiated for the task you're trying to acheive but just for learning purposes I'll write the correct version of your snippet

string separator = string.Empty; // starts empty so doesn't apply for first element foreach (string s in words) { txtParse.Text += separator + s; // You need to use += operator to append content separator = " "; // from second element will append " " } 

EDIT

This is for the case of using different labels

Label[] labelList = new Label[] {label1, label2, label3, label4, label5}; for (int i = 0; i < words.Length; i++) { labelList[i].Text = words[i]; } 

9 Comments

it says that: Index was outside the bounds of the array. I type your code + the fixed one from the question.
@NikolaObretenov: will the text to split always 5 elements?
It will split mostly on 5 (now i try with 5 elements)
@Nikola Obreteno: Could you debug and tell me on which element this code fails?
element: labelList[i].Text = words[i]; type: Index was outside the bounds of the array.
|
1

You can use String.Join to join collection of strings as you want. In your case:

txtParse.Text = String.Join(" ", words); 

Comments

1

You can use the delimiter character directly in .split('-') to return an array of strings representing that data you want.

Comments

1

Your problem is that you keep assigning s to the Text property. The end result will be the last s in your array.

You can use TextBox.AppendText() instead.

 char[] delemiterChars = { '-' }; string text = txtProbe.Text; string[] words = text.Split(delemiterChars); txtParse.Text = text; foreach (string s in words) { txtParse.AppendText(s + " "); } 

3 Comments

That worked instead of that i want to set them now in labels ... as i edited my question :|
@NikolaObretenov you could add your Label controls to an array and do a for loop to set the Text property of each array. However, this is really a different question than what you originally asked. You might consider posting a new question to cover that.
So i need to add a neew question ... Roger that. Thanks to all
0

You can just put txtParse.Text = txtProbe.Text.Replace('-', " ");

Or by modifying your code:

char[] delemiterChars = { '-' }; string text = txtProbe.Text; string[] words = text.Split(delemiterChars,StringSplitOptions.RemoveEmptyEntries); foreach (string s in words) { txtParse.Text += " " + s; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.