Substring from character to character in C#

前端 未结 3 1900
再見小時候
再見小時候 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条回答
  •  鱼传尺愫
    2021-01-21 23:45

    If you string has always one , and one ; (and ; after your ,), you can use combination of IndexOf and Substring like;

    string someString = "1.7,2015-05-21T09:18:58;";
    int index1 = someString.IndexOf(',');
    int index2 = someString.IndexOf(';');
    
    someString = someString.Substring(index1 + 1, index2 - index1 - 1);
    Console.WriteLine(someString); // 2015-05-21T09:18:58
    

    Here a demonstration.

提交回复
热议问题