Get string between two strings in a string

前端 未结 23 2413
别跟我提以往
别跟我提以往 2020-11-22 09:46

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 betw

23条回答
  •  萌比男神i
    2020-11-22 10:38

    I used the code snippet from Vijay Singh Rana which basically does the job. But it causes problems if the firstString does already contain the lastString. What I wanted was extracting a access_token from a JSON Response (no JSON Parser loaded). My firstString was \"access_token\": \" and my lastString was \". I ended up with a little modification

    string Between(string str, string firstString, string lastString)
    {    
        int pos1 = str.IndexOf(firstString) + firstString.Length;
        int pos2 = str.Substring(pos1).IndexOf(lastString);
        return str.Substring(pos1, pos2);
    }
    

提交回复
热议问题