Novkovski Stevo Bato
Novkovski Stevo Bato

Reputation: 1043

Extract strings from string

Ill lose to much time since i don`t have too much experience in manipulating with strings/chars.

i have

string original = "1111,2222,"This is test work")";

i need

string first = "1111";
string second = "2222";
string name = "This is test work";

C# ASP.NET

Upvotes: 0

Views: 737

Answers (2)

Abe Miessler
Abe Miessler

Reputation: 85056

Use the String.Split method:

string[] values = original.Split(new Char [] {','});

This will break apart your string at every comma and return a string array containing each part. To access:

string first = values[0];
string second = values[1];
string name = values[2];

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160912

Use string.Split() - your pattern is simple (split on comma), there is no need to use a RegEx here:

var parts = original.Split(',');
first = parts[0];
second = parts[1];
name = parts[2].TrimEnd(')'); //in case you really wanted to remove that last bracket

Upvotes: 3

Related Questions