Split using Environment.NewLine

  • Thread starter Thread starter Vamsi
  • Start date Start date
V

Vamsi

Hi,
I am trying a basic opearation of splitting a multiline value to an
array of single lines(Actually making Address into AddressLine1,
AddressLine2).
I used Environment.NewLine in split, I could get only 1st line, but it
is not returning 2nd line.
here's code:
string[] address = null;
string ssep = Environment.NewLine;
char[] sep = ssep.ToCharArray();
address = this.AddressLine1.Split(sep);
this.AddressLine1=address[0].ToString();
this.AddressLine2=address[1].ToString();

Am I doing anything wrong?
Thanks,
Vamsi
 
Vamsi,

NewLine will return the carriage-feed/newline combo. Are you sure that
your lines are delimited with this combo, or is it delimited only by the
newline character?

Hope this helps.
 
Vamsi,
There are three Split functions in .NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Alternatively use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

If you are using C#, you can reference the Microsoft.VisualBasic.dll
assembly to use the first function.

As Nicholas stated, Environment.NewLine is a string (two characters) you may
want to consider using the VB function or the Regex Method.

Hope this helps
Jay
 
Your approach seems solid. The following test code works:

string words = "test\n\ntest\ntest\n";

string [] split = words.Split
(Environment.NewLine.ToCharArray());

foreach (string s in split) {
if (s.Trim() != "")
Response.Write("<p>" + s);
}


JER
 
Environment.NewLine ?

Been searching for that for ages.


Jerry Negrelli said:
Your approach seems solid. The following test code works:

string words = "test\n\ntest\ntest\n";

string [] split = words.Split
(Environment.NewLine.ToCharArray());

foreach (string s in split) {
if (s.Trim() != "")
Response.Write("<p>" + s);
}


JER

-----Original Message-----
Hi,
I am trying a basic opearation of splitting a multiline value to an
array of single lines(Actually making Address into AddressLine1,
AddressLine2).
I used Environment.NewLine in split, I could get only 1st line, but it
is not returning 2nd line.
here's code:
string[] address = null;
string ssep = Environment.NewLine;
char[] sep = ssep.ToCharArray();
address = this.AddressLine1.Split(sep);
this.AddressLine1=address[0].ToString();
this.AddressLine2=address[1].ToString();

Am I doing anything wrong?
Thanks,
Vamsi
.
 
We can also do following,

string words = "test\n\ntest\ntest\n";

char[] token = new
char[]{System.Environment.NewLine.ToCharArray()[0]};
string [] split = words.Split(token);

foreach (string s in split) {
Response.Write("<p>" + s.Trim());
}

Regards,
-Maulin




Alvin said:
Environment.NewLine ?

Been searching for that for ages.


Your approach seems solid. The following test code works:

string words = "test\n\ntest\ntest\n";

string [] split = words.Split
(Environment.NewLine.ToCharArray());

foreach (string s in split) {
if (s.Trim() != "")
Response.Write("<p>" + s);
}


JER

-----Original Message-----
Hi,
I am trying a basic opearation of splitting a multiline value to an
array of single lines(Actually making Address into AddressLine1,
AddressLine2).
I used Environment.NewLine in split, I could get only 1st line, but it
is not returning 2nd line.
here's code:
string[] address = null;
string ssep = Environment.NewLine;
char[] sep = ssep.ToCharArray();
address = this.AddressLine1.Split(sep);
this.AddressLine1=address[0].ToString();
this.AddressLine2=address[1].ToString();

Am I doing anything wrong?
Thanks,
Vamsi
.
 
Back
Top