jorame
jorame

Reputation: 2207

How to break a string at each comma?

Hi guys I have a problem at hand that I can't seem to figure out, I have a string (C#) which looks like this:

string tags = "cars, motor, wheels, parts, windshield";

I need to break this string at every comma and get each word assign to a new string by itself like:

string individual_tag = "car";

I know I have to do some kind of loop here but I'm not really sure how to approach this, any help will be really appreciate it.

Upvotes: 1

Views: 4347

Answers (6)

Damith
Damith

Reputation: 63065

You can use one of String.Split methods

Split Method (Char[])
Split Method (Char[], StringSplitOptions)
Split Method (String[], StringSplitOptions)

let's try second option: I'm giving , and space as split chars then on each those character occurrence input string will be split, but there can be empty strings in the results. we can remove them using StringSplitOptions.RemoveEmptyEntries parameter.

string[] tagArray = tags.Split(new char[]{',', ' '},
                               StringSplitOptions.RemoveEmptyEntries);

OR

 string[] tagArray = s.Split(", ".ToCharArray(), 
                               StringSplitOptions.RemoveEmptyEntries);

you can access each tag by:

foreach (var t in tagArray )
{
    lblTags.Text = lblTags.Text + " " + t; // update lable with tag values 
    //System.Diagnostics.Debug.WriteLine(t); // this result can be see on your VS out put window 
}

Upvotes: 7

Fahim Parkar
Fahim Parkar

Reputation: 31637

Program that splits on spaces [C#]

using System;
class Program
{
    static void Main()
    {
        string s = "there, is, a, cat";
        string[] words = s.Split(", ".ToCharArray());
    foreach (string word in words)
    {
        Console.WriteLine(word);
    }
    }
}

Output

there
is
a
cat

Reference

Upvotes: 3

Justin Niessner
Justin Niessner

Reputation: 245419

No loop needed. Just a call to Split():

var individualStrings = tags.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

Upvotes: 10

Josh Mein
Josh Mein

Reputation: 28635

You are looking for the C# split() function.

string[] tags = tags.Split(',');

Edit:

string[] tag = tags.Trim().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

You should definitely use the form supplied by Justin Niessner. There were two key differences that may be helpful depending on the input you receive:

  1. You had spaces after your ,s so it would be best to split on ", "

  2. StringSplitOptions.RemoveEmptyEntries will remove the empty entry that is possible in the case that you have a trailing comma.

Upvotes: 4

Pranay Rana
Pranay Rana

Reputation: 176896

make use of Split function will do your task...

string[] s = tags.Split(',');

or

String.Split Method (Char[], StringSplitOptions)

char[] charSeparators = new char[] {',',' '};
string[] words = tags.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);

Upvotes: 7

srini.venigalla
srini.venigalla

Reputation: 5145

string[] words = tags.Split(',');

Upvotes: 4

Related Questions