How to split a string having "," and ";" in c# [Resolved]

Posted by Kasani007 under C# on 1/19/2016 | Points: 10 | Views : 1913 | Status : [Member] | Replies : 4
How to split a string having "," and ";" in c#

i have a string i.e.,

string str ="rama,chandra;kumar"

how can i split the string having 2 special characters (i.e, "," and ";")




Responses

Posted by: Rajnilari2015 on: 1/19/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Kasani007 Sir, a little addition to what Sheo Sir has already presented,

Way 1: Using Split function of System.String class

"rama,chandra;kumar"
.Split(new Char[] { ',', ';' },StringSplitOptions.RemoveEmptyEntries)
.ToList()
.ForEach(i => Console.WriteLine(i));


Way 2: Using Split function of System.Text.RegularExpressions.Regex class

Regex
.Split("rama,chandra;kumar", @"[,;]+")
.ToList()
.ForEach(i => Console.WriteLine(i));


Way 3: Replace and split

 new Regex("[,;]")
.Replace("rama,chandra;kumar", ",") //replacing with comma
.Split(',') //split with comma
.ToList()
.ForEach(i => Console.WriteLine(i));


In all the above cases, the output will be

rama 
chandra
kumar


Hope the answers presented here will help you.

--
Thanks & Regards,
RNA Team

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

Posted by: Sheonarayan on: 1/19/2016 [Administrator] HonoraryPlatinum | Points: 25

Up
1
Down
Here you go

string str = "rama,chandra;kumar";
char[] delims = { ',', ';' };
var strings = str.Split(delims);
foreach(var s in strings)
{
Response.Write(s + "<br />");
}

This will print. We are basically passing array of characters to split from in Split function.

rama
chandra
kumar

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

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

Posted by: Amatya on: 2/12/2016 [Member] Silver | Points: 25

Up
0
Down
Good Deeds.. :)

Feel free to share informations.
mail Id ' adityagupta200@gmail.com
Thanks

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

Login to post response