Get string between two strings in a string

flow picture flow · Jun 22, 2013 · Viewed 258.5k times · Source

I have a string like:

"super exemple of string key : text I want to keep - end of my string"

I want to just keep the string which is between "key : " and " - ". How can I do that? Must I use a Regex or can I do it in another way?

Answer

Dmitry Bychenko picture Dmitry Bychenko · Jun 22, 2013

Perhaps, a good way is just to cut out a substring:

String St = "super exemple of string key : text I want to keep - end of my string";

int pFrom = St.IndexOf("key : ") + "key : ".Length;
int pTo = St.LastIndexOf(" - ");

String result = St.Substring(pFrom, pTo - pFrom);