【问题标题】:How to insert a "," in between each char of a string如何在字符串的每个字符之间插入“,”
【发布时间】:2015-04-03 15:32:19
【问题描述】:

在我的程序中,我需要在字符串中设置标记,然后再通过另一种方法传递它,我有它,以便每个第 4 个字符都有一个“|”插入,这是为了标记一个换行符。不是我想把每个字符都放在外部标记“|”之间并放一个“,”。这两个 char 数组方法在这里不起作用,否则我会尝试使用它,但我不是在寻找 char 数组。

public static String matrixFormatter(String x){

x = x.substring(0, 4) + "|" + x.substring(4, x.length());
    return x;
 }

到目前为止这工作,现在我想在每个字符之间添加一个“,”,我认为下面的代码可以工作,这很容易但我错了。

public static String matrixFormatter(String x){

for(int i = 0; i<=x.length(); i+=4){
    for(int j = 0; j<=x.length(); i++){
    x = x.substring(0, i) + "|" + x.substring(i, x.length());
    x = x.substring(0, j) + "|" + x.substring(j, x.length());
    }
}
return x;

}

【问题讨论】:

    标签: java string loops split substring


    【解决方案1】:

    下面的代码在字符串中的字符之间添加了一个“,”。

    public static String matrixFormatter(String x){
          String result;
          for(int i = 0; i<x.length()-1; i++){
                 result += x.substring(i, i+1) + ",";
           }
          return result+",";
    }
    

    【讨论】:

      【解决方案2】:

      试试这个正则表达式

          s = s.replaceAll("(?<=.)(?=.)", ",");
      

      【讨论】:

      • 想解释一下正则表达式吗? (VLQRQ)
      【解决方案3】:

      可以使用StringBufferJoinerGuava 库:

      public static void main(String[] args) {
          String s = "example";
          System.out.println(withBuilder(s));
          System.out.println(withJoiner(s));
      }
      
      private static String withJoiner(String s) {
          return Joiner.on(",").join(Chars.asList(s.toCharArray()));
      }
      
      private static String withBuilder(String s)
      {
          StringBuilder builder = new StringBuilder(s);
          int index = 1;
          for (int i = 0; i < s.length() ; i++)
          {
              builder.insert(index, ",");
              index +=2;
          }
          return builder.toString();
      }
      

      输出是:

      e,x,a,m,p,l,e,
      e,x,a,m,p,l,e
      

      【讨论】:

        【解决方案4】:

        可以通过一种方法完成:

        public static String matrixFormatter(String x) {
            List<String> chars = Arrays.asList(x.split(""));
            String result = chars.get(0);
            for (int i = 1; i < chars.size(); i++) {
                if (i % 4 == 0)
                    result += "|" + chars.get(i);
                else
                    result += "," + chars.get(i);
            }
            return result;
        }
        

        调用:

        System.out.println(matrixFormatter("12345678"));
        

        输出:

        1,2,3,4|5,6,7,8
        

        【讨论】:

          【解决方案5】:
          public static String matrixFormatter(String x) {
            resultstr = "";
            int i = 0;
            while(i < x.length()) {
              // If end of string: only add character.
              if (i == x.length() - 1) {
                resultstr += x.substring(i, i + 1);
              } else {
                if ( ((i + 1) % 4) == 0) {
                  resultstr += x.substring(i, i + 1)  + "|";
                } else {
                  resultstr += x.substring(i, i + 1)  + ",";
                }
              }
              i++;
            }
            return resultstr;
          }
          

          尚未安装 Java,但通过 PHP 代码测试了该概念:

          function matrixFormatter($x) {
            $resultstr = "";
            $i = 0;
            while($i < strlen($x)) {
              if ($i == strlen($x) - 1) {
                $resultstr .= $x[$i];
              } else {
                if ( (($i + 1) % 4) == 0) {
                  $resultstr .= $x[$i] . "|";
                } else {
                  $resultstr .= $x[$i] . ",";
                }
              }
              $i++;
            }
            return $resultstr;
          }
          

          matrixFormatter("abcdefghijklmnopq") 返回 "a,b,c,d|e,f,g,h|i,j,k,l|m,n,o,p|q"。

          【讨论】:

            【解决方案6】:

            我不确定我是否正确理解了您的问题,您可能应该添加一些输入和预期输出以更清晰。

                String a = "abcdefghijklmnop";
                String a2 = "";
                for (int i = 0; i < a.length(); i++) {
                    if (i != 0) {
                        if(i % 4 == 0){
                           a2 += "|";
                        } else{
                           a2 += ",";
                        }
                    }
                    a2 += a.charAt(i);
                }
                System.out.println(a2);
            

            这将产生输出a,b,c,d|e,f,g,h|i,j,k,l|m,n,o,p

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-03-16
              • 1970-01-01
              • 1970-01-01
              • 2015-12-12
              • 2015-04-04
              • 1970-01-01
              相关资源
              最近更新 更多