developer033
developer033

Reputation: 24894

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;
}

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

Upvotes: 0

Views: 666

Answers (5)

hiral
hiral

Reputation: 1

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

Upvotes: 0

AdamSkywalker
AdamSkywalker

Reputation: 11619

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();
}

Upvotes: 4

ninja.coder
ninja.coder

Reputation: 9658

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

Upvotes: 0

Hasan
Hasan

Reputation: 86

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

Upvotes: 0

Yassin Hajaj
Yassin Hajaj

Reputation: 22005

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;

Upvotes: 0

Related Questions