How to Read Substrings based on Length of the String in C#

前端 未结 5 1663
有刺的猬
有刺的猬 2021-01-23 16:06

I have a String which contains a substrings One of them is \"1 . 2 To Other Mobiles\" and other is \"Total\".Now as per my requirement i have to read the Contents between first

5条回答
  •  梦谈多话
    2021-01-23 17:06

    Problem : you are not adding the length of the first search string 1 . 2 To Other MobilessubstringTotal

    Solution :

    string currentText = "1 . 2 To Other MobilessubstringTotal";
    string search1="1 . 2 To Other Mobiles";
    string search2="Total";
    int startPosition = currentText.IndexOf(search1);
    int endPosition = currentText.IndexOf(search2);
    string result = currentText.Substring((startPosition+search1.Length), endPosition - (startPosition+search1.Length));
    

    Output:

    result => substring

提交回复
热议问题