i wanna split the string using the vertical bar spliter ( || - pipe delimiter). in my string, this delimiter occurs 4 times, which means, i have to get the result as string array[3]. but i am getting string array[7]. where it adds an empty array between
every string which is splitted already..
For example, my code is lik
string temp = "INDIA || is || a || Developing || Country";
string[] arTemp = temp.split("||".ToCharArray());
in the above code, my result should be wounded until arTemp[3], coz, string is splitted into 4 parts. but, i am getting arTemp[9]. Between each splitter array string it is adding one empty string, except for the last string.
output is coming like
arTemp[0] = "INDIA"
arTemp[1] = ""
arTemp[2] = "is"
arTemp[3] = ""
arTemp[4] = "a"
arTemp[5] = ""
arTemp[6] = "Developing "
arTemp[7] = ""
arTemp[8] = "Country"
How to remove the empty spaces in between the array stringS??
temp.Split("||".ToCharArray()); is equivalent to temp.Split('|');
You can probably get the result you want by using the StringSplitOptions enum (or it might be SplitStringOptions) anyway there is a choice for ignoring null values.
Sorry for the inexact syntax but without intellisense I'm lost.
Marked as answer by mbanavige on May 06, 2012 11:18 PM
string temp = "INDIA || is || a || Developing || Country";
your delimiter character of | occurs 8 times.
abivenkat
temp.split("||".ToCharArray());
and when you split on a character array of || (2 pipes in a row) you are essentiually telling the split method to split on | or |. Is does not instruct the Split method to split on the string ||.
The Split method always splits on individual characters.
abivenkat
Member
50 Points
97 Posts
string split Delimiter doubt
May 05, 2012 12:59 PM|LINK
hi all,
i wanna split the string using the vertical bar spliter ( || - pipe delimiter). in my string, this delimiter occurs 4 times, which means, i have to get the result as string array[3]. but i am getting string array[7]. where it adds an empty array between every string which is splitted already..
For example, my code is lik
string temp = "INDIA || is || a || Developing || Country";
string[] arTemp = temp.split("||".ToCharArray());
in the above code, my result should be wounded until arTemp[3], coz, string is splitted into 4 parts. but, i am getting arTemp[9]. Between each splitter array string it is adding one empty string, except for the last string.
output is coming like
arTemp[0] = "INDIA"
arTemp[1] = ""
arTemp[2] = "is"
arTemp[3] = ""
arTemp[4] = "a"
arTemp[5] = ""
arTemp[6] = "Developing "
arTemp[7] = ""
arTemp[8] = "Country"
How to remove the empty spaces in between the array stringS??
thanks,
abivenkat
sunilgurjar
Contributor
2252 Points
478 Posts
Re: string split Delimiter doubt
May 05, 2012 01:06 PM|LINK
string[] arTemp = temp.Trim().split("||".ToCharArray());read my blog
http://sunilgurjar.blogspot.com/
RichardY
Star
8376 Points
1573 Posts
Re: string split Delimiter doubt
May 05, 2012 01:06 PM|LINK
temp.Split("||".ToCharArray()); is equivalent to temp.Split('|');
You can probably get the result you want by using the StringSplitOptions enum (or it might be SplitStringOptions) anyway there is a choice for ignoring null values.
Sorry for the inexact syntax but without intellisense I'm lost.
abivenkat
Member
50 Points
97 Posts
Re: string split Delimiter doubt
May 05, 2012 01:52 PM|LINK
thanks for ur suggestions ...
i found that the below line code is the perfect solution for my issue.
string[] artemp = tem.Split(new string[] {"||"}, StringSplitOptions.RemoveEmptyEntries);
this worked...
thanks,
abivenkat
mbanavige
All-Star
134961 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: string split Delimiter doubt
May 06, 2012 11:23 PM|LINK
Just a small follow up but... in this string
your delimiter character of | occurs 8 times.
and when you split on a character array of || (2 pipes in a row) you are essentiually telling the split method to split on | or |. Is does not instruct the Split method to split on the string ||.
The Split method always splits on individual characters.