【问题标题】:split String If get any capital letterssplit String 如果得到任何大写字母
【发布时间】:2021-09-04 13:32:55
【问题描述】:

我的字符串: BByTTheWay 。我想拆分字符串 as B By T The Way BByTheWay 。这意味着如果我得到任何大写字母,我想拆分字符串,最后按原样放置主字符串。到目前为止,我在 java 中尝试过:

public String breakWord(String fileAsString) throws FileNotFoundException, IOException {

    String allWord = "";
    String allmethod = "";
    String[] splitString = fileAsString.split(" ");
    for (int i = 0; i < splitString.length; i++) {
        String k = splitString[i].replaceAll("([A-Z])(?![A-Z])", " $1").trim();
        allWord = k.concat(" " + splitString[i]);
        allWord = Arrays.stream(allWord.split("\\s+")).distinct().collect(Collectors.joining(" "));
        allmethod = allmethod + " " + allWord;
        //  System.out.print(allmethod);
    }
    return allmethod;

}

它给了我输出: B ByT The Way BByTTheWay。我认为 stackoverflow 社区可以帮助我解决这个问题。

【问题讨论】:

    标签: java regex


    【解决方案1】:

    您可以使用此代码:

    代码 1

    String s = "BByTTheWay";
    Pattern p = Pattern.compile("\\p{Lu}\\p{Ll}*");
    
    String out = p.matcher(s)
         .results()
         .map(MatchResult::group)
         .collect(Collectors.joining(" "))
         + " " + s;
    
    //=> "B By T The Way BByTTheWay"
    

    RegEx \\p{Lu}\\p{Ll}* 匹配任何 unicode 大写字母后跟 0 个或多个小写字母。

    CODE DEMO


    或者使用 String.split 使用相同的正则表达式,稍后再加入:

    代码 2

    String out = Arrays.stream(s.split("(?=\\p{Lu})"))
        
    .collect(Collectors.joining(" ")) + " " + s;
    //=> "B By T The Way BByTTheWay"
    

    【讨论】:

    • 这里 .results() 显示 找不到符号 ..我使用的是 java 版本“1.8.0_111”-
    • ok 那是版本不兼容,你可以试试我的Code 2
    • 仅供参考,您的两个解决方案都可以成功使用变音符号。 String s = "ÀLaCarte";.
    【解决方案2】:

    使用

    String s = "BByTTheWay";
    Pattern p = Pattern.compile("[A-Z][a-z]*");
    Matcher m = p.matcher(s);
    String r = "";
    while (m.find()) {
        r = r + m.group(0) + " ";
    }
    System.out.println(r + s);
    

    Java proof

    结果B By T The Way BByTTheWay

    解释

    --------------------------------------------------------------------------------
      [A-Z]                    any character of: 'A' to 'Z'
    --------------------------------------------------------------------------------
      [a-z]*                   any character of: 'a' to 'z' (0 or more
                               times (matching the most amount possible))
    

    【讨论】:

    • 感谢您的回答。您提供的代码非常容易理解
    • 使用变音符号失败。 String s = "ÀLaCarte";
    • @BasilBourque 一点也不失败,BByTTheWay 没有变音符号。该表达式完全符合 OP 代码尝试,无需猜测。 Sanzida,如果变音符号是个问题,请告知。
    【解决方案3】:

    根据要求,checking if a character is an alphabet可以这样写,也可以不写:

    char[] chars = fileAsString.toCharArray();
    StringBuilder fragment = new StringBuilder();
    for (char ch : chars) {
        if (Character.isLetter(ch) && Character.isUpperCase(ch)) { // it works as internationalized check
            fragment.append(" ");
        }
        fragment.append(ch);
    }
    String.join(" ", fragment).concat(" " + fileAsString).trim(); // B By T The Way BByTTheWay
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 2013-02-04
      • 1970-01-01
      • 2011-04-23
      • 2013-05-11
      相关资源
      最近更新 更多