【问题标题】:How to concatenate this string properly?如何正确连接此字符串?
【发布时间】:2017-05-25 23:37:37
【问题描述】:
String[] str = {"this", "is", "a", "test", "."}

我要输出:

"this is a test."

StringBuilder sb = new StringBuilder();
for(int i=0; i<str.length; i++){
  sb.append(str[i] + " ");
}

但是,这将输出:“这是一个测试。”。如何正确避免点前的空格?

【问题讨论】:

  • 我认为我们需要一个更通用的规则。在什么情况下你不想要空间?仅当下一个数组元素恰好是 "."?
  • 假设我们需要一个一般条件,比如最后一个字符不需要空格。
  • 是的。几个标点符号。
  • 你需要更具体。不清楚何时需要空间,何时不需要。

标签: java


【解决方案1】:

你可以在 concat 操作后只删除最后一个字符之前的空格。

public static void main(String[] args) {
    String[] str = {"this", "is", "a", "test", "."};

    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < str.length; i++){
        sb.append(str[i] + " ");
    }
    sb.deleteCharAt(sb.length() - 3);
    System.out.println(sb);
}

【讨论】:

    【解决方案2】:

    我不是 Java 开发人员,但这里是快速和肮脏的(抱歉任何语法违规)

    StringBuilder sb = new StringBuilder();
    for(int i=0; i<str.length; i++){
      if(i==str.length-1 || i==0){
        sb.append(str[i]);
      }else{
        sb.append(" "+str[i]);
      }
    }
    

    对于第一项或最后一项,只需将值添加到字符串中,否则添加空格和值。

    【讨论】:

    • 最好是这样的。
    • 很高兴能帮上忙!
    【解决方案3】:

    技术上this is a test . 是正确的,如果您的规则是将字符串连接起来并在其间留有空格。如果您希望句点和逗号等语法符号没有空格,则必须执行一些正则表达式。此外,您实际上并不需要循环或 StringBuilders:

    String[] array = new String[] { "this", "is", "a", "test", "." };
    String joined = String.join(" ", array);
    joined = joined.replaceAll(" ([.,])", "$1");
    

    类似的,其中第一个 replaceAll() 参数是一个正则表达式,其中包含您在括号内关心的所有符号

    【讨论】:

      【解决方案4】:

      使用正则表达式,您可以找到标点符号并避免最后一个空格,例如,如果您有以下数组:

      {"this", "is", "a", "test", ".", "and", "this", "is", "another", 
       "test", ";", "here", "is", "one", "test", "more", "!"};
      

      所以,你可以这样实现:

      private static String regex = "[.!;?\\-]";
      
      String[] str = {"this", "is", "a", "test", ".", "and", "this", "is",
              "another", "test", ";", "here", "is", "one", "test", "more", "!"};
      
      StringBuilder sb = new StringBuilder();
      
      for (int i = 0; i < str.length; i++) {
          if (str[i].matches(regex)) {
              sb.deleteCharAt(sb.length() - 1);
          }
          sb.append(str[i] + " ");
      }
      System.out.println(sb.toString());
      

      输出:

      this is a test. and this is another test; here is one test more! 
      

      【讨论】:

        【解决方案5】:
        public String generateSentence(String[] str) {
            StringBuilder builder = new StringBuilder(str[0]);
            for (String word: str) {
                if (word.equals(".")) {
                    builder.append(word);
                } else {
                    builder.append(" ").append(word);
                }
            }
            return builder.toString();
        }
        

        【讨论】:

        • 这将创建超出边界的索引
        • 我知道。你打字比我快,所以我没能及时更新。
        • 我记得有比这更好的方法。以某种方式在要添加的标记之前添加空格,以某些特殊字符为条件
        • 你在哪些场景中会出现点字符?
        • 只是几个标点符号,前面不应该有空格。
        【解决方案6】:

        试试这个 java 8 版本:

                String[] str = {"this", "is", "a", "test", "."};
            String test = Arrays.stream(str).reduce(new String (), (p,q) -> p+" "+q).replace(" .", ".");
            System.out.println("out ::" + test);
        

        输出:

        out :: 这是一个测试。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-23
          • 1970-01-01
          • 1970-01-01
          • 2017-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多