-4

I want to replace Space with "-" what is the way

Suppose my code is

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

I want output

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

thanks

user207421
  • 305,947
  • 44
  • 307
  • 483
Adi
  • 1
  • 1
  • 5

7 Answers7

4

Like this,

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

and like this as well

System.out.println(tokenstr.toString().replaceAll("\\s+", "-"));
Vinay
  • 6,891
  • 4
  • 32
  • 50
0

If you have the StringBuffer object then you need to iterate it and replace the character:

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

or convert it into String then replace as below :

String value = tokenstr.toString().replaceAll(" ", "-");
Kick
  • 4,823
  • 3
  • 22
  • 29
0

Do like this

 StringBuffer tokenstr=new StringBuffer();
 tokenstr.append("technician education systems of the Cabinet has approved the following".replace(" ", "-"));
 System.out.print(tokenstr);
Nambi
  • 11,944
  • 3
  • 37
  • 49
0

You may try this :

//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);
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
0

you need to write custom replaceAll method. Where you need to find out src string index and replace those string sub-string with destination string.

Please find a code snippet by Jon Skeet

Community
  • 1
  • 1
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

If you dont want to jump back and forth between StringBuffer and String classes, you can do this:

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,"-");
}
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
0

/You can use below method pass your String parameter and get result as String spaces replaced with hyphen /

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