Split a String in C#.Net

Posted by Naraayanan under C# on 7/12/2012 | Points: 10 | Views : 3939 | Status : [Member] | Replies : 6
Hi friends,
How to split a Empty string in C#.net?
This is my program.

string str = "";
int str1 = str .Split(',').Length;

int value is 1.
why it return 1?Please explain it.

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/



Responses

Posted by: Premalatha on: 7/12/2012 [Member] Starter | Points: 25

Up
0
Down
string phrase = "The quick brown fox";
string[] words;

words = phrase.Split(default(Char[]), StringSplitOptions.RemoveEmptyEntries);

words = phrase.Split((char[]) null, StringSplitOptions.RemoveEmptyEntries);

words = phrase.Split(null as char[], StringSplitOptions.RemoveEmptyEntries);
http://www.dotnetperls.com/split

Premalatha
Software Engineer

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Calvert on: 7/12/2012 [Member] Starter | Points: 25

Up
0
Down
The Split

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 7/12/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Thanks for your reply.Please try my example and explain me.

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Megan00 on: 7/13/2012 [Member] Starter | Points: 25

Up
0
Down

string str = "abcd";
string[] substrs = str.Split(',');
int length = substrs.Length;
Console.WriteLine(length.ToString());
Console.ReadLine();


Actually, if the string "abcd" can not be seperated by ",". It can be regarded as whole. so it return "1". but if the "," are included in the string, it can be split. such as if the code is:

           string str = "a,b,c,d";

string[] substrs = str.Split(',');
int length = substrs.Length;
Console.WriteLine(length.ToString());
Console.ReadLine();

it return as 4. if only "a,b,c" it returns 3. in your code, even it is empty, but it is a string, the string is taken as a whole, so it returns 1.

Never give up! Smile to the world!
http://excelcsharp.blog.com/

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Calvert on: 7/14/2012 [Member] Starter | Points: 25

Up
0
Down
To split a string at a separator character, use the Index Of or Index Of Any method to locate a separator character in the string.If you are splitting a string at a separator string, use the Index Of or Index Of Any method to locate the first character of the separator string. Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
http://www.dapfor.com/en/net-suite/net-grid/tutorial/data-sorting


Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Naraayanan on: 7/15/2012 [Member] Starter | Points: 25

Up
0
Down
HI,
Thanks

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Naraayanan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response