0

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.

developer033
  • 24,267
  • 8
  • 82
  • 108
  • 2
    That's exactly what `.toLowerCase()` does. You need to manipulate each match. – SLaks Mar 06 '16 at 15:18
  • Since you want to check the match per word, consider to split the string and checking the match per word instead of the whole string at once. – Ian Mar 06 '16 at 15:19
  • @SLaks, of course I know what `.toLowerCase()` does, but I want to lowerCase only word that matches the `Regex`. – developer033 Mar 06 '16 at 15:22
  • Then you apparently don't know how expressions work. – Chris Kitching Mar 06 '16 at 15:26
  • Possible duplicate of [Use Java and RegEx to convert casing in a string](http://stackoverflow.com/questions/2770967/use-java-and-regex-to-convert-casing-in-a-string) – Justin Mar 06 '16 at 15:38

5 Answers5

4

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();
}
developer033
  • 24,267
  • 8
  • 82
  • 108
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
  • Thanks for the response. I tested the first input and it returns me wrong result: "José DAs SIlVa", it should be "José das Silva". – developer033 Mar 06 '16 at 15:44
  • 1
    @developer033 oh, I see, you need to lowercase these words, I'll update the code then – AdamSkywalker Mar 06 '16 at 15:46
  • Now it is working perfectly, you just forgot a thing (toLowerCase) in the end. Instead it won't lowercase the rest of chars in String. Thanks. – developer033 Mar 06 '16 at 16:00
0

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;
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

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

Hasan
  • 86
  • 8
0

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
user2004685
  • 9,548
  • 5
  • 37
  • 54
0
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

hiral
  • 1
  • 1