Replace the last part of a string

The following code should replace the last occurrence of a ‘,’ with a ‘)’. StringBuilder b = new StringBuilder(yourString); b.replace(yourString.lastIndexOf(“,”), yourString.lastIndexOf(“,”) + 1, “)” ); yourString = b.toString(); Note This will throw Exceptions if the String doesn’t contain a ‘,’.

Case insensitive string replacement in JavaScript?

You can use regular expressions if you prepare the search string. In PHP e.g. there is a function preg_quote, which replaces all regex-chars in a string with their escaped versions. Here is such a function for javascript (source): function preg_quote (str, delimiter) { // discuss at: https://locutus.io/php/preg_quote/ // original by: booeyOH // improved by: Ates … Read more

How does MatchEvaluator in Regex.Replace work?

A MatchEvaluator is a delegate that takes a Match object and returns a string that should be replaced instead of the match. You can also refer to groups from the match. You can rewrite your code as follows: string input = “23x * y34x2″; Regex reg = new Regex(@”\d(?=[a-z])|[a-z](?=\d)”); string result = reg.Replace(input, delegate(Match m) … Read more