Remove characters after specific character in string, then remove substring in C#?
To Remove characters after specific character in String, we useLastIndexOf()
method and then use Substring()
method which will split the string based on a delimiter.Example
using System;
public class Program
{
public static void Main()
{
string input = "http://www.somesite.com/somepage.aspx?whatever";
int index = input.LastIndexOf("/");
if (index >= 0)
input = input.Substring(0, index); // or index + 1 to keep slash
Console.WriteLine(input);
}
}
Output
http://www.somesite.com