Reputation: 65
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:
Upvotes: 0
Views: 144
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
Reputation: 1
You can you this:
string[] mystrings = mystring.Split('.');
string secondString = strings[1];
Upvotes: 0
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