Get string between two strings in a string

前端 未结 23 2510
别跟我提以往
别跟我提以往 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条回答
  •  忘掉有多难
    2020-11-22 10:49

    You already have some good answers and I realize the code I am providing is far from the most efficient and clean. However, I thought it might be useful for educational purposes. We can use pre-built classes and libraries all day long. But without understanding the inner-workings, we are simply mimicking and repeating and will never learn anything. This code works and is more basic or "virgin" than some of the others:

    char startDelimiter = ':';
    char endDelimiter = '-';
    
    Boolean collect = false;
    
    string parsedString = "";
    
    foreach (char c in originalString)
    {
        if (c == startDelimiter)
             collect = true;
    
        if (c == endDelimiter)
             collect = false;
    
        if (collect == true && c != startDelimiter)
             parsedString += c;
    }
    

    You end up with your desired string assigned to the parsedString variable. Keep in mind that it will also capture proceeding and preceding spaces. Remember that a string is simply an array of characters that can be manipulated like other arrays with indices etc.

    Take care.

提交回复
热议问题