You need to sign in to do that
Don't have an account?
mjohnson-TIC
Ignore Case using Replace
Does anyone know of any easy methods to ignore case of a string literal doing a replace? For instance:
string a = 'one';
string b = 'OneTwoThree';
string c = b.replace(a,'Four');
The above replace would not work due to case sensitivity because string a = 'one' and not 'One'.
Is there some way of ignoring the case on string a?
you can use the toLowerCase or toUpperCase to convert the string, get the indexOf, and then get the substring:
aLower = a.toLowerCase();
bLower = b.toLowerCase();
index = bLower.indexOf(aLower);
aActual = b.substring(index, a.length());
c = b.replace(aActual, 'Four');
here is a ready to go method for replacing while ignoring case:
you need to use a Regex to get what you need.
As per documentation, String Apex Class has these two methods which allow to use regex:
- replaceAll(regExp, replacement)
- replaceFirst(regExp, replacement)
This works using your example: The syntax "(?i)" before a string, makes the regex case insensitive.Check this website (https://www.regular-expressions.info/modifiers.html) for more info.