0

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"

skrk
  • 65
  • 2
  • 9

3 Answers3

5

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"
JamieT
  • 1,177
  • 1
  • 9
  • 19
3
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

JamieT
  • 1,177
  • 1
  • 9
  • 19
JM123
  • 167
  • 1
  • 12
0

You can you this:

string[] mystrings = mystring.Split('.');
string secondString = strings[1];
sticky bit
  • 36,626
  • 12
  • 31
  • 42