What is the best way to replace a string only at the end?
If I have a string such as "abc 123 def 456 abc" - I only want to replace
abc at the end. Is there a way?
If the question is "Is there a built-in Framework method to do this" then
the answer is No.
You could use a regular expression to find all the occurrences of your
pattern and then get the location of the last item in the Matches
collection.
You could also reverse the string (AND the search pattern!) and use
IndexOf() to find the pattern and then between that, the length of the
string, and the length of the search pattern you could determine its
position in the original string.
And then there's the looping way of using IndexOf() to find the pattern and
continuing to use it (with the overload that specifies the start position of
the search) to keep looking until you don't find the pattern anymore. The
last one you find is...duh...the last one in the string.