【问题标题】:How do i add a "\n" every 6 spaces in a string?如何在字符串中每 6 个空格添加一个“\n”?
【发布时间】:2014-01-02 18:08:22
【问题描述】:

我在多维数组中有很长的字符串。我一直在尝试找出一种方法,用"\n" 替换每个字符串中的每 6 个空格,这将导致它基本上按回车键。

示例:
orginalString="我想在 this 的第六个空格后面输入 enter";

FormatedString = "我想在\n这个第六个空格之后输入"

这是我到目前为止得到的。我可能完全错了。

public static void FormatArray(String c) {
    int counter = 0;
    for (int i = 0; i < c.length(); i++) {
        if (c.charAt(i) == ' ') {
            counter++;
        }
        if (counter == 6) {
            counter = 0;
            for (int j = 0; j < Variables.getCards().length; j++) {
                StringBuilder string = new StringBuilder(
                                                    Variables.getCards()[j][1]);
                string.setCharAt(i, '\n');
                System.out.println(string);
            }
        }
    }
}

【问题讨论】:

    标签: java arrays string replace split


    【解决方案1】:

    我会这样做

    // given a String str, replace every sixth space with " \n "
    public static String formatString(String str) {
      if (str == null) {                         // Handle null.
        return null;
      }
      StringBuilder sb = new StringBuilder();    // An output buffer.
      int count = 0;
      for (char ch : str.toCharArray()) {        // loop over the characters.
        if (ch == ' ') {                         // test for space.
          count++;
        }
        if (count == 6) {                        // every sixth space.
          count = 0;
          sb.append(" \n");
        }
        sb.append(ch);
      }
      return sb.toString();                      // return the string.
    }
    
    // Test it.
    public static void main(String[] args) {
      String originalString = "i want to put enter after the sixth space of this";
      String formattedString = "i want to put enter after \n the sixth space of this";
      if (formatString(originalString).equals(
          formattedString)) {
        System.out.println("Yes");
      } else {
        System.out.println("No");
      }
    }
    

    当我运行上面的代码时,我得到了输出 -

    Yes
    

    【讨论】:

      【解决方案2】:

      你说的很对。我建议如下:

      public static String formatStr(String a){
          if(a==null) return null;
          String newStr="";
          int len=a.length();
          int counter=0;
          for(int i=0;i<len;i++){
              newStr+=a.charAt(i);
              if (a.charAt(i) == ' ')counter++;
      
              if (counter == 6){
                  counter=0;
                  newStr+="\n";
                  //System.out.println(string);
              }
          }
          return newStr;
      }
      

      然后你可以调用formatStr(whatever),你会得到返回的字符串。

      【讨论】:

        【解决方案3】:

        您可以使用以下正则表达式:

            String pattern = "(\\S*\\s){6}";
            String testString = "This is just a regular test. This is just a regular test. This is just a regular test.";
        
            Pattern p = Pattern.compile(pattern);
            Matcher matcher = p.matcher(testString);
            StringBuilder strBuilder = new StringBuilder();
        
            while (matcher.find()) {
                strBuilder.append(matcher.group());
                strBuilder.replace(strBuilder.length() - 1, strBuilder.length(), "\n"); // To make it platform independent, user System.lineSeparator() instead of "\n" - only works in Java 7
            }
        
            if (matcher.hitEnd()) {
                strBuilder.append(testString.substring(strBuilder.length()));
            }
        
            String newString = strBuilder.toString();
            System.out.println(newString);
        

        输出将是:

        This is just a regular test.
        This is just a regular test.
        This is just a regular test.
        

        【讨论】:

          【解决方案4】:

          这是一个单一的解决方案:

          str = str.replaceAll("(\\S+\\s+){6}", "$0\n");
          

          替换术语$0 是整个匹配项,因此这会将匹配项放回原处加上换行符。

          如果您不喜欢正则表达式,只需更改它即可。例如,如果您想用换行符替换第 6 个空格,请从捕获中排除尾随空格:

          str = str.replaceAll("((\\S+\\s+){5}\\S+)\\s+", "$1\n");
          

          【讨论】:

            猜你喜欢
            • 2013-12-13
            • 1970-01-01
            • 2014-04-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-07-15
            相关资源
            最近更新 更多