Java 中的全部替换

ReplaceAll in Java

我正在使用 WordsUtilscapitalize 个单词。

由于我无法定义哪些单词应该大写,我不得不在capitalize函数之后再做一个实现,将一些单词小写。

应该小写的单词是:["da, de, di, do, du, das, des, dis, dos, dus"].

那么,我现在的 code 是:

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

问题是 replaceAll 将每个单词都小写,而不仅仅是匹配 Pattern.

的介词

尝试通过在使用正则表达式和 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", "").toLowerCase() : 
          newWord;

Java8 没有第三方库的解决方案:

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

所以您希望所有单词都大写,但您指定的单词除外? 或者您不希望单词大写,如果该单词与指定的单词匹配,那么您想将其转换为小写?

第一种情况: 您需要注意并确定是否要将以下字母小写 das 或任何包含该词的词 达萨达达 如果仅与您指定的字词完全匹配,则

Str.matches("firstword|secondword");

或者如果有任何包含这些词的词 Str.matches("(.*)firstword(.*)|(.*)secondword(.*)");

第二种情况: 那么你不需要 String newWord = WordUtils.capitalizeFully(word);

您正在通过 newWord.replaceAll("\b([d|D][a-zA-Z]{1,2})\b", "").toLowerCase(); 将整个字符串转换为小写。您应该只将匹配项转换为小写。

下面是代码片段,它首先将输入字符串转换为大写,然后查找每个匹配项并将其转换为小写。

代码片段:

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

输入:

josé dAs sIlVa

输出:

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

输出:

何塞·达斯·席尔瓦

乔治·德·保拉

保拉斯的玛丽亚