+ Start a Discussion
mjohnson-TICmjohnson-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?

willardwillard

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');

 

 

Peter van der Meij | StepOrangePeter van der Meij | StepOrange
for anyone else running into this post.

here is a ready to go method for replacing while ignoring case:
 
public String replaceIgnoreCase(String replace, String with, String inText) {
        String replaceLower = replace.toLowerCase();
        String inTextLower = inText.toLowerCase();
        Integer indexOfTag = inTextLower.indexOf(replaceLower);
        if(indexOfTag > -1) {
            String replaceAsInText = inText.substring(indexOfTag, (indexOfTag+replace.length()));
            inText = inText.replace(replaceAsInText, with);
            inText = replaceIgnoreCase(replace, with, inText);
        }
        return inText;
    }

 
Davide BuosoDavide Buoso
Hi mjohnson-TIC,

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:
String a = 'one';
String b = 'OneTwoThree';
String c = b.replaceFirst('(?i)' + a, 'Four');
System.debug(c); // => "FourTwoThree"
The syntax "(?i)" before a string, makes the regex case insensitive.
Check this website (https://www.regular-expressions.info/modifiers.html) for more info.
 
jack helljack hell
Online games are very popular at present. The mini militia mod apk is a wildly popular hacked version of the war-themed Doodle Army 2 game. Find the list of best mini militia mods here. https://medium.com/@minimilitiaapk/doodle-army-2-mini-militia-mod-apk-download-686cf97f42ac
nick frednick fred
Nice information and you explained in a detailed way. Thanks for sharing this to us. https://volunteerinsrilanka.net/mynordstrom/
freefirecash freefirecashfreefirecash freefirecash
The excellent way to get free fire diamonds is that you may earn unfastened Google Play credit through on line surveys and purchase loose hearth diamonds with the help of those money. In this, a few apps offering online survey can help you to top-up diamonds without spending a dime without spending a unmarried penny.  Free Fire Diamonds As we advised you in advance that we are not sharing any type of diamond hack, script or any cheat. Here we've observed a few prison methods with the aid of which you may purchase diamond with out spending your hard earned money. You most effective ought to deploy few incomes apps and use them nicely.

 
Aman Singh 170Aman Singh 170
The excellent way to get free fire diamonds is that you may earn unfastened Google Play credit through on line surveys and purchase loose hearth diamonds with the help of those money. In this, a few apps offering online survey can help you to top-up diamonds without spending a dime without spending a unmarried penny Dish TV Recharge Plan (https://tricks.nayag.com/dish-tv-recharge-plan/) s we advised you in advance that we are not sharing any type of diamond hack, script or any cheat. Here we've observed a few prison methods with the aid of which you may purchase diamond with out spending your hard earned money. You most effective ought to deploy few incomes apps and use them nicely.
 
Aman Singh 170Aman Singh 170
e advised you in advance that we are not sharing (https://www.google.co.kr/url?q=https://tricks.nayag.com/vodafone-free-internet/)any type of diamond hack, script or any cheat. Here we've observed a few prison methods with the aid of which you may purchase
Dhananjay soniDhananjay soni
The excellent way to get free fire diamonds is that you may earn unfastened Google Play credit through on line surveys and purchase loose hearth diamonds with the help of those money. In this, a few apps offering online survey can help you to top-up diamonds without spending a dime without spending a unmarried penny.  Free Fire Diamond Generator (https://pointofgamer.com/free-fire-diamond-generator/) As we advised you in advance that we are not sharing any type of diamond hack, script or any cheat. Here we've observed a few prison methods with the aid of which you may purchase diamond with out spending your hard earned money. You most effective ought to deploy few incomes apps and use them nicely.