Hoang Nam
Hoang Nam

Reputation: 111

How to skip case while using String replaceAll

Example my string have :

text = " Hi {fullname}, wellcome u join my group"

this is my code:

text = text.replaceAll("\\{fullname\\}", user.getMobile() != null ? user.getMobile() : "");

but now i have problem if {fullname} is {FULLNAME} or {Fullname} or {fuLLname}, i cant find exactly what i want and replace it. Anyone have a solution please help me! Thanks

Upvotes: 1

Views: 186

Answers (3)

Hemant Singh Bisht
Hemant Singh Bisht

Reputation: 322

You can use (?i) as a prefix of your token "fullname" which will make it case insensitive so it will work for any Upper and Lower case combination of your token "fullname" "{FULLname} , {fullNAME} , {FulLName} ,{fuLLname}"

text = text.replaceAll("\\{(?i)fullname\\}", user.getMobile() != null ? user.getMobile() : "");

Upvotes: 3

viraj nilakh
viraj nilakh

Reputation: 91

Try using this

    String ans="Hi {fullname}, wellcome u join my group";

    ans = ans.replaceAll("\\{[a-zA-Z]*\\}", "ANYTHING");

    System.out.println(ans);

Upvotes: -1

Suresh Atta
Suresh Atta

Reputation: 121998

ReplaceAll takes regex, so you can try (?i) ignorecase in regex

text = text.replaceAll("\\{(?i)fullname\\}", us...

That works for all {fullname} , {FULLNAME} , {Fullname} ,{fuLLname}

Upvotes: 2

Related Questions