简体   繁体   中英

If I write String.toLowerCase, it won't work, but I need it, how can I solve?

ArrayList listaTesti is defined as global variable

the cambiaValore function takes 2 Strings from a textField

the find Function takes the 2 Strings from the textField, and it should replace all the occurrences of the "testoDaModificare" with "conCheParola" I've included String.toLowerCase, so, if the user insert a uppercase value, it doesn't matter.

if I don't write "string.toLowerCase" it work, but if the user put a uppercase value while it there isn't, the function will not work.

private void cambiaValore(String testoDaModificare, String conCheParola)
{

    ArrayList <String> appoggio = cerca(testoDaModificare, conCheParola); 
    int i = 0;
    listaTesti.removeAll(listaTesti); //Rimuovo tutti gli elementi della lista 
    for (String string : appoggio) //E li ri assegno utilizzando quelli modificati
    {
        String temp = appoggio.get(i); 
        listaTesti.add(temp);
        i++;
    }
}

private ArrayList <String> cerca(String testoDaCambiare,String conCheParola)
{
    int i = 0;
    ArrayList <String> appoggio = new ArrayList();
    for(String testo : listaTesti)
    {

        if(listaTesti.get(i).toLowerCase().contains(testoDaCambiare.toLowerCase()))
        {
            String testo3 = listaTesti.get(i).replaceAll(testoDaCambiare.toLowerCase(), conCheParola);
            appoggio.add(testo3);
            i++;
        }
        else
        {
            appoggio.add(listaTesti.get(i));
            i++;
        }

    }
    return appoggio;
}

The issue is here:

String testo3 = listaTesti.get(i).replaceAll(testoDaCambiare.toLowerCase(), conCheParola);

The value returned from get(i) has not been converted to lower case. You need to tell replaceAll() to ignore case.

String testo3 = listaTesti.get(i).replaceAll("(?i)" + testoDaCambiare, conCheParola);

The "(?i)" tells replaceAll to ignore case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM