skrk
skrk

Reputation: 65

C# code to find a string between two strings - same identical strings

C# code to get the string between two strings

example :

mystring = "aaa.xxx.b.ccc.12345"

Need c# code to get the second string "xxx" between two ".", always the second string ignore other strings between the "." What is the best way to get "xxx" out of "aaa.xxx.b.ccc.12345"

And the second set of string can be anything eg:

"aaa.123.b.ccc.12345" "aaa.re.b.ccc.45" "eee.stt.b.ccc.ttt" "233.y.b.ccc.5"

Upvotes: 0

Views: 144

Answers (3)

JM123
JM123

Reputation: 187

mystring.Split('.').Skip(1).FirstOrDefault();

We split at each '.' and we ignore the first then take the first one.

We need handling of nulls. if not just use First

Upvotes: 3

Alessio Ciarrocchi
Alessio Ciarrocchi

Reputation: 1

You can you this:

string[] mystrings = mystring.Split('.');
string secondString = strings[1];

Upvotes: 0

JamieT
JamieT

Reputation: 1187

We can use string.Split() get an array of all strings delimited by the parameter you pass it. For example:

var strings = mystring.Split('.');
// strings = {"aaa", "xxx", "b", "ccc", "12345"}

var str = strings[1];
// str = "xxx"

Upvotes: 5

Related Questions