How to get substring before a string in C#

1 Answer

0 votes
using System;

class Program
{
    static string StringBefore(string str, string a) {
        int posA = str.IndexOf(a);

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

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

        Console.WriteLine(StringBefore(str, "Java"));
    }
}




/*
run:

C#:C C++:

*/

 



answered Oct 30, 2022 by avibootz
...