【问题标题】:How to replace space with hyphen?如何用连字符替换空格?
【发布时间】:2014-03-16 08:01:54
【问题描述】:

我想把空格换成“-”是什么方法

假设我的代码是

StringBuffer tokenstr=new StringBuffer();
tokenstr.append("technician education systems of the Cabinet has approved the following");

我要输出

"technician-education-systems-of-the-Cabinet-has-approved-the-following"

谢谢

【问题讨论】:

  • 你可以使用String.replaceAll()
  • @user2573153 我使用的是字符串缓冲区而不是字符串。
  • 你试过了吗?
  • StringBuffer.toString().replaceAll(...)?
  • @RC 我没听明白?

标签: java replace stringbuffer


【解决方案1】:

/您可以使用下面的方法传递您的字符串参数并以连字符替换字符串空格获得结果 /

 private static String replaceSpaceWithHypn(String str) {
    if (str != null && str.trim().length() > 0) {
        str = str.toLowerCase();
        String patternStr = "\\s+";
        String replaceStr = "-";
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(str);
        str = matcher.replaceAll(replaceStr);
        patternStr = "\\s";
        replaceStr = "-";
        pattern = Pattern.compile(patternStr);
        matcher = pattern.matcher(str);
        str = matcher.replaceAll(replaceStr);
    }
    return str;
}

【讨论】:

    【解决方案2】:

    如果你有 StringBuffer 对象,那么你需要迭代它并替换字符:

     for (int index = 0; index < tokenstr.length(); index++) {
                if (tokenstr.charAt(index) == ' ') {
                    tokenstr.setCharAt(index, '-');
                }
            }
    

    或将其转换为字符串,然后替换如下:

    String value = tokenstr.toString().replaceAll(" ", "-");
    

    【讨论】:

      【解决方案3】:

      如果不想在 StringBuffer 和 String 类之间来回跳转,可以这样做:

      StringBuffer tokenstr = new StringBuffer();
      tokenstr.append("technician education systems of the Cabinet has approved the following");
      
      int idx=0;
      while( idx = tokenstr.indexOf(" ", idx) >= 0 ) {
          tokenstr.replace(idx,idx+1,"-");
      }
      

      【讨论】:

        【解决方案4】:

        您需要编写自定义replaceAll 方法。您需要在哪里找到 src 字符串索引并将那些字符串子字符串替换为目标字符串。

        请通过Jon Skeet查找代码sn-p

        【讨论】:

          【解决方案5】:

          你可以试试这个:

          //First store your value in string object and replace space with "-" before appending it to StringBuffer. 
          String str = "technician education systems of the Cabinet has approved the following";
          str = str.replaceAll(" ", "-");
          StringBuffer tokenstr=new StringBuffer();
          tokenstr.append(str);
          System.out.println(tokenstr);
          

          【讨论】:

            【解决方案6】:

            这样做

             StringBuffer tokenstr=new StringBuffer();
             tokenstr.append("technician education systems of the Cabinet has approved the following".replace(" ", "-"));
             System.out.print(tokenstr);
            

            【讨论】:

              【解决方案7】:

              这样,

              StringBuffer tokenstr = new StringBuffer();
              tokenstr.append("technician education systems of the Cabinet has approved the following");
              System.out.println(tokenstr.toString().replaceAll(" ", "-"));
              

              还有这样的

              System.out.println(tokenstr.toString().replaceAll("\\s+", "-"));
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-07-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多