Ok, so I have a multi-line textbox on my form.
1. How can I add each line on the textbox into an array.
2. But I want to omit empty lines, so no partition is empty.
Textbox Input:
Line 1
Line 2
Line 4
So the string array would have 3 partitions
myArray[0] = Line 1
myArray[1] = Line 2
myArray[2] = Line 4
Just another small question about strings,
string word = "Testing";
txtoutput.Text = word[0].ToString();
That outputs "T", word[1] would output "e"
But can I do something like word[1-3] which would output "est"??
btw I'm new to C#
2 replies to this topic
#1
Posted 19 August 2010 - 03:30 PM
|
|
|
#2
Posted 19 August 2010 - 09:29 PM
As for your first question, here's a sample on how you could accomplish something like this. A TextBox (when set to multiline) has a property Lines Property. This property holds a string array of all the lines in the multiline TextBox control. We can create a for loop then length of the number of lines in your TextBox, on each iteration check to make sure the current line isn't empty, if it's not then add it to our string array
You could also go this route:
Use Regular Expressions to remove all the empty lines, then just assign your string array to the Lines property of the multiline textbox
There are many other ways of accomplishing this. I personally would go with a List<string> for this and not a string array. Here's an example of using a generic list over a string array. So we create a List<string> to hold each line in the TextBox. If the current line isnt empty or null we add it to our generic list. Finally we convert our generic list to a string array
So now at least you have a couple options to choose from. Hope I was able to help :)
private void Form1_Load(object sender, EventArgs e)
{
//create a string array the size of number of lines in TextBox
string[] myArray = new string[textBox1.Lines.Count()];
//now loop for each line in the TextBox
for (int i = 0; i < textBox1.Lines.Count(); i++)
{
//check to amke sure the line isnt empty or null
//if not then add it to our array
if (!string.IsNullOrEmpty(textBox1.Lines[i]))
{
myArray[i] = textBox1.Lines[i];
}
}
}
You could also go this route:
Use Regular Expressions to remove all the empty lines, then just assign your string array to the Lines property of the multiline textbox
textBox1.Text = Regex.Replace(textBox1.Text, "(?<Text>.*)(?:[\r\n]?(?:\r\n)?)", "${Text}\r\n");
string[] myArray = textBox1.Lines;
There are many other ways of accomplishing this. I personally would go with a List<string> for this and not a string array. Here's an example of using a generic list over a string array. So we create a List<string> to hold each line in the TextBox. If the current line isnt empty or null we add it to our generic list. Finally we convert our generic list to a string array
private void Form1_Load(object sender, EventArgs e)
{
List<string> lines = new List<string>();
//now loop for each line in the TextBox
for (int i = 0; i < textBox1.Lines.Count(); i++)
{
//check to amke sure the line isnt empty or null
//if not then add it to our array
if (!string.IsNullOrEmpty(textBox1.Lines[i]))
{
lines.Add(textBox1.Lines[i]);
}
}
//now for fun convert the generic list to a string array
string[] myArray = lines.ToArray();
}
So now at least you have a couple options to choose from. Hope I was able to help :)
#3
Posted 20 August 2010 - 07:49 PM
txtbox.Lines()
So simple, if I only knew about it before. Thanks for the tip
So simple, if I only knew about it before. Thanks for the tip
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









