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"

3
  • 1
    mystring.Split('.')[1]? Commented Apr 29, 2019 at 20:47
  • Possible duplicate of Get string between two strings in a string Commented Apr 29, 2019 at 20:50
  • I don't get the point of "same identical strings". what string is be the same as what string? Commented Apr 29, 2019 at 20:52

3 Answers 3

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"
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

You can you this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.