Search:
Namespaces
Discussions
.NET v1.1
Feedback
Splitting items in a text box
Messages
Related Types
This message was discovered on
ASPFriends.com 'aspngregexp' list
.
dexter
I want to split items in a multiline textbox, using the newline (\n) as the
separator.
So the following is in a text box:
"John"
"Paul"
"George"
"Ringo"
which should be separated into the array:
{"John", "Paul", "George", "Ringo"}
Instead I am getting:
{"John", "", "Paul", "", "George", "", "Ringo"}
In other words, the carriage return is being outputted in the output array.
I am using the following code to split the textbox input:
=============================================
//split on newline
Regex
r = new
Regex
("(\n)");
string[] strNames = r.Split(textboxNames.Text);
=============================================
What should I be using as the separator text?
Thanks
Reply to this message...
Wayne King (ASP.NET)
Get rid of the superfluous grouping parens, as:
Regex
r =3D new
Regex
("\n");
For the Split method of
Regex
, capturing groups are used to add
additional strings to the output array.
Also, since your split expression is so simple (just one character), it
would be a lot more efficient to just use the string object's built-in
split method, as:
string[] strNames =3D textboxNames.Text.Split('\n');
-----Original Message-----
From: dexter [mailto:
Click here to reveal e-mail address
]=20
Sent: Wednesday, July 10, 2002 10:14 AM
To: aspngregexp
Subject: [aspngregexp] Splitting items in a text box
I want to split items in a multiline textbox, using the newline (\n) as
the separator.
So the following is in a text box:
"John"
"Paul"
"George"
"Ringo"
which should be separated into the array:
{"John", "Paul", "George", "Ringo"}
Instead I am getting:
{"John", "", "Paul", "", "George", "", "Ringo"}
In other words, the carriage return is being outputted in the output
array.
I am using the following code to split the textbox input:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
//split on newline
Regex
r =3D new
Regex
("(\n)");
string[] strNames =3D r.Split(textboxNames.Text);
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
What should I be using as the separator text?
Thanks
Reply to this message...
dexter
Thanks for this.
control.Text.Split() is pretty cool.
Unfortunately when I use this expression, I get:
{"John", "\n", "Paul", "\n", "George", "\n", "Ringo"}
That is to say, a carriage return character gets returned. I've tried an
assortment of other combinations of delimiters, including:
string[] strDomains = txtDomainName.Text.Split(new
Char
[] {'\n', '\n', '
',});
But in this case I get:
{"John\n", "Paul\n", "George\n", "Ringo"}
Any other help here to separate to get:
{"John", "Paul", "George", "Ringo"}
?
Many Thanks
"Wayne King (ASP.NET)" <
Click here to reveal e-mail address
> wrote in message
news:681689@aspngregexp...
Get rid of the superfluous grouping parens, as:
Regex
r = new
Regex
("\n");
For the Split method of
Regex
, capturing groups are used to add
additional strings to the output array.
Also, since your split expression is so simple (just one character), it
would be a lot more efficient to just use the string object's built-in
split method, as:
string[] strNames = textboxNames.Text.Split('\n');
-----Original Message-----
From: dexter [mailto:
Click here to reveal e-mail address
]
Sent: Wednesday, July 10, 2002 10:14 AM
To: aspngregexp
Subject: [aspngregexp] Splitting items in a text box
I want to split items in a multiline textbox, using the newline (\n) as
the separator.
So the following is in a text box:
"John"
"Paul"
"George"
"Ringo"
which should be separated into the array:
{"John", "Paul", "George", "Ringo"}
Instead I am getting:
{"John", "", "Paul", "", "George", "", "Ringo"}
In other words, the carriage return is being outputted in the output
array.
I am using the following code to split the textbox input:
=============================================
//split on newline
Regex
r = new
Regex
("(\n)");
string[] strNames = r.Split(textboxNames.Text);
=============================================
What should I be using as the separator text?
Thanks
Reply to this message...
Wayne King (ASP.NET)
It sounds like your input string is not what you think it is. Here's a
simple program that compares various approaches:
------------------------------------------------
using System;
using System.Text.RegularExpressions;
public class SplitTest {
public static void Main() {
const string ss =3D "John\nPaul\nGeorge\nRingo";
Console.WriteLine("ss: " + ss.Replace("\n", "\\n") + '\n');
Console.WriteLine( @"Regex.Split(ss, ""\n"")" );
writeIt(
Regex
.Split(ss, "\n"));
Console.WriteLine( @"Regex.Split(ss, ""\\n"")" );
writeIt(
Regex
.Split(ss, "\\n"));
Console.WriteLine( @"Regex.Split(ss, ""(\\n)"")" );
writeIt(
Regex
.Split(ss, "(\\n)"));
Console.WriteLine("ss.Split('\\n')");
writeIt(ss.Split('\n'));
Console.WriteLine("ss.Split('\\n', '\\n', ' ')");
writeIt(ss.Split('\n', '\n', ' '));
Console.WriteLine("ss.Split(new char {'\\n', '\\n', ' '})");
writeIt(ss.Split(new char[] {'\n', '\n', ' '}));
Console.WriteLine("ss.Split()");
writeIt(ss.Split());
}
static void writeIt(string[] sa)
{
Console.WriteLine("length array: " + sa.Length.ToString());
foreach(string word in sa)
Console.WriteLine(" word: " + word.Replace("\n",
"\\n"));
Console.WriteLine();
}
}
------------------------------------------
-Wayne
This posting is provided "AS IS" with no warranties, and confers no
rights.
-----Original Message-----
From: dexter [mailto:
Click here to reveal e-mail address
]=20
Sent: Thursday, July 11, 2002 4:39 AM
To: aspngregexp
Subject: [aspngregexp] Re: Splitting items in a text box
Thanks for this.
control.Text.Split() is pretty cool.
Unfortunately when I use this expression, I get:
{"John", "\n", "Paul", "\n", "George", "\n", "Ringo"}
That is to say, a carriage return character gets returned. I've tried an
assortment of other combinations of delimiters, including: string[]
strDomains =3D txtDomainName.Text.Split(new
Char
[] {'\n', '\n', ' ',});
But in this case I get:
{"John\n", "Paul\n", "George\n", "Ringo"}
Any other help here to separate to get:
{"John", "Paul", "George", "Ringo"}
?
Many Thanks
"Wayne King (ASP.NET)" <
Click here to reveal e-mail address
> wrote in message
news:681689@aspngregexp...
Get rid of the superfluous grouping parens, as:
Regex
r =3D new
Regex
("\n");
For the Split method of
Regex
, capturing groups are used to add
additional strings to the output array.
Also, since your split expression is so simple (just one character), it
would be a lot more efficient to just use the string object's built-in
split method, as:
string[] strNames =3D textboxNames.Text.Split('\n');
-----Original Message-----
From: dexter [mailto:
Click here to reveal e-mail address
]
Sent: Wednesday, July 10, 2002 10:14 AM
To: aspngregexp
Subject: [aspngregexp] Splitting items in a text box
I want to split items in a multiline textbox, using the newline (\n) as
the separator.
So the following is in a text box:
"John"
"Paul"
"George"
"Ringo"
which should be separated into the array:
{"John", "Paul", "George", "Ringo"}
Instead I am getting:
{"John", "", "Paul", "", "George", "", "Ringo"}
In other words, the carriage return is being outputted in the output
array.
I am using the following code to split the textbox input:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
//split on newline
Regex
r =3D new
Regex
("(\n)");
string[] strNames =3D r.Split(textboxNames.Text);
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
What should I be using as the separator text?
Thanks
Reply to this message...
System.Char
System.Console
System.Text.RegularExpressions.Regex
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com