Remove last occurrence of a string in a string

The String.LastIndexOf method does what you need - returns the last index of a char or string.

If you're sure that every string will have at least one set of parentheses:

var result = node.Text.Substring(0, node.Text.LastIndexOf("("));

Otherwise, you could test the result of LastIndexOf:

var lastParenSet = node.Text.LastIndexOf("(");

var result =
    node.Text.Substring(0, lastParenSet > -1 ? lastParenSet : node.Text.Count());

This should do what you want :

your_string = your_string.Remove(your_string.LastIndexOf(string_to_remove));

It's that simple.

Tags:

C#

String