.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification