Substring from character to character in C#

前端 未结 3 1902
再見小時候
再見小時候 2021-01-21 23:06

How can I get sub-string from one specific character to another one?

For example if I have this format:

string someString = \"1.7,2015-05-21T09:18:58;\";         


        
3条回答
  •  闹比i
    闹比i (楼主)
    2021-01-21 23:53

    This would be better:

    string input = "1.7,2015-05-21T09:18:58;";
    string output = input.Split(',', ';')[1];
    

    Using SubString:

    public string FindStringInBetween(string Text, string FirstString, string LastString)
    {
        string STR = Text;
        string STRFirst = FirstString;
        string STRLast = LastString;
        string FinalString;
    
        int Pos1 = STR.IndexOf(FirstString) + FirstString.Length;
        int Pos2 = STR.IndexOf(LastString);
    
        FinalString = STR.Substring(Pos1, Pos2 - Pos1);
        return FinalString;
    } 
    

    Try:

    string input = "1.7,2015-05-21T09:18:58;";
    string output = FindStringInBetween(input, ",", ";");
    

    Demo: DotNet Fiddle Demo

提交回复
热议问题