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.

asked Mar 6, 2016 at 15:15
5
  • 2
    That's exactly what .toLowerCase() does. You need to manipulate each match. Commented Mar 6, 2016 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. Commented Mar 6, 2016 at 15:19
  • @SLaks, of course I know what .toLowerCase() does, but I want to lowerCase only word that matches the Regex. Commented Mar 6, 2016 at 15:22
  • Then you apparently don't know how expressions work. Commented Mar 6, 2016 at 15:26
  • Possible duplicate of Use Java and RegEx to convert casing in a string Commented Mar 6, 2016 at 15:38

5 Answers 5

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
25k8 gold badges85 silver badges113 bronze badges
answered Mar 6, 2016 at 15:32
Sign up to request clarification or add additional context in comments.

3 Comments

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 oh, I see, you need to lowercase these words, I'll update the code then
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.
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;
answered Mar 6, 2016 at 15:31

Comments

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

answered Mar 6, 2016 at 15:34

Comments

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
answered Mar 6, 2016 at 15:40

Comments

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

answered Mar 6, 2016 at 16:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.