Java 如何用连字符替换空格?

Java 如何用连字符替换空格?,java,replace,stringbuffer,Java,Replace,Stringbuffer,我想用“-”来代替空格,怎么办 假设我的代码是 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

我想用“-”来代替空格,怎么办

假设我的代码是

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"

谢谢

如果您有StringBuffer对象,则需要迭代它并替换字符:

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

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+", "-"));
你喜欢这样吗

 StringBuffer tokenstr=new StringBuffer();
 tokenstr.append("technician education systems of the Cabinet has approved the following".replace(" ", "-"));
 System.out.print(tokenstr);
你可以试试这个:

//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);

您需要编写自定义的
replaceAll
方法。您需要找出src字符串索引,并用目标字符串替换这些字符串子字符串


请通过

查找代码片段如果不想在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,"-");
}
/您可以使用下面的方法传递字符串参数,并将结果作为字符串空格替换为连字符/

 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;
}

您可以使用
String.replaceAll()
@user2573153我使用的是Stringbuffer而不是String。您试过什么吗?
Stringbuffer.toString().replaceAll(…)
?@RC我没听懂您的意思?
 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;
}