Omar Rayo
Omar Rayo

Reputation: 23

how to get text after a certain comma on C#?

Ok guys so I've got this issue that is driving me nuts, lets say that I've got a string like this "aaa,bbb,ccc,ddd,eee,fff,ggg" (with out the double quotes) and all that I want to get is a sub-string from it, something like "ddd,eee,fff,ggg".

I also have to say that there's a lot of information and not all the strings look the same so i kind off need something generic.

thank you!

Upvotes: 1

Views: 860

Answers (3)

Alex K.
Alex K.

Reputation: 175956

One way using split with a limit;

string str = "aaa,bbb,ccc,ddd,eee,fff,ggg";

int skip = 3;

string result = str.Split(new[] { ',' }, skip + 1)[skip];

// = "ddd,eee,fff,ggg"

Upvotes: 1

Seth Kitchen
Seth Kitchen

Reputation: 1566

Not really sure if all things between the commas are 3 length. If they are I would use choice 2. If they are all different, choice 1. A third choice would be choice 2 but implement .IndexOf(",") several times.

Two choices:

string yourString="aaa,bbb,ccc,ddd,eee,fff,ggg";
string[] partsOfString=yourString.Split(','); //Gives you an array were partsOfString[0] is "aaa" and partsOfString[1] is "bbb"
string trimmed=partsOfString[3]+","+partsOfString[4]+","+partsOfString[5]+","+partsOfSting[6];

OR

//Prints "ddd,eee,fff,ggg"
string trimmed=yourString.Substring(12,14) //Gets the 12th character of your string and goes 14 more characters.

Upvotes: 0

Rob10e
Rob10e

Reputation: 319

I would use stringToSplit.Split(',')

Update:

var startComma = 3;    
var value = string.Join(",", stringToSplit.Split(',').Where((token, index) => index > startComma));

Upvotes: 0

Related Questions