简体   繁体   中英

ReplaceAll in Java

I'm using WordsUtils to capitalize words.

Since I can't define which words should be capitalized, I have to make another implementation after capitalize function to put some words lowercase.

The words that should be lowercase are: ["da, de, di, do, du, das, des, dis, dos, dus"] .

So, my code at the moment is:

public static String capitalize(String word) {
   String newWord = WordUtils.capitalizeFully(word);
   newWord = newWord.replaceAll("\\b([d|D][a-zA-Z]{1,2})\\b", "$1").toLowerCase();
   return newWord;
}
  • Example of inputs:

    1. josé dAs sIlVa
    2. Jorge De PAuLa
    3. MaRiA DAS PauLas

The problem is that the replaceAll is puttng every word lowercase , not only the prepositions that matches the Pattern .

Java8 solution without third party libs:

public static void main(String[] args) {
    String str = "hello mY dEAr friends";
    Set<String> ban = new HashSet<>(Arrays.asList("my", "dear"));
    String result = Arrays.stream(str.split("\\s"))
                          .map(s -> capitalize(s, ban))
                          .collect(Collectors.joining(" "));
    System.out.println(result);
}

static String capitalize(String s, Set<String> ban) {
    String lc = s.toLowerCase();
    return ban.contains(lc) ? lc 
                            : Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase();
}

Try putting a condition by checking if the word is a target before using the regex and toLowerCase

List<String> str = Arrays.asList("da, de, di, do, du, das, des, dis, dos, dus".split(", "));
newWord = str.contains(word) ? 
          newWord.replaceAll("\\b([d|D][a-zA-Z]{1,2})\\b", "$1").toLowerCase() : 
          newWord;

so you want all the word to be capitalized but the words you specified ? or you want no word to be capitalized and if the word matches one of specified then you like to convert it to lowercase ?

first case : you need to take care and determine if you want to lowercase the letters of das or any word that contain that word like dasadada if only specifically matches the word you specified then

Str.matches("firstword|secondword");

or if any word that contains those words Str.matches("(.*)firstword(.*)|(.*)secondword(.*)");

second case: then you dont need String newWord = WordUtils.capitalizeFully(word);

You are converting the whole String to lower case by doing newWord.replaceAll("\\\\b([d|D][a-zA-Z]{1,2})\\\\b", "$1").toLowerCase(); . You should only convert the matches to lower case.

Below is the code snippet which first converts the input string to upper case and then find & convert each match to the lower case.

Code Snippet:

public static void main(String[] args) {
    String str = "josé dAs sIlVa".toUpperCase();
    Matcher m = Pattern.compile("D(A|E|I|O|U|AS|ES|IS|OS|US)").matcher(str);

    while(m.find()) {
        String match = m.group(0);
        str = str.replace(match,match.toLowerCase());
    }

    System.out.println(str);
}

Input:

josé dAs sIlVa

Output:

JOSÉ daS SILVA
class MyClass
{
    public static void main (String[] args) throws java.lang.Exception
    {

       String[] wordArray = {"jose dAs sIlVa","Jorge De PAuLa","MaRiA DAS PauLas"};
       for(int i=0;i<wordArray.length;i++){
        System.out.println(capitalize(wordArray[i]));   
       }
     }

    static String capitalize(String word) {
             if(word!=null && word!=""){
               String[] wordArray = word.trim().split(" ");
               word= "";
               for(int i=0;i<wordArray.length;i++){
                   String currentWord = wordArray[i].trim();
                   if(currentWord.matches("\\b([d|D][a-zA-Z]{1,2})\\b")){
                       currentWord = currentWord.toLowerCase();
                   }else{
                       currentWord = currentWord.toUpperCase();
                   }
                   word = word+" "+currentWord;
               }
           }
           return word.trim();
        }
}

Output:

JOSE das SILVA

JORGE de PAULA

MARIA das PAULAS

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