How to get substring after a string in C#

1 Answer

0 votes
using System;
 
class Program
{
    static string StringAfter(string str, string a) {
        int posA = str.LastIndexOf(a);

        if (posA == -1)
            return "";

        if (posA + a.Length >= str.Length)
            return "";

        return str.Substring(posA + a.Length);
    }
    static void Main() {
        string str = "C#.NET:C C++:Java";

        Console.WriteLine(StringAfter(str, "NET"));
    }
}



 
/*
run:
 
:C C++:Java
 
*/

 



answered Oct 30, 2022 by avibootz
...