Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing space with %20 . 1

Status
Not open for further replies.

Petal

Programmer
Jun 8, 2001
56
NO
I have made this replace function:

public String replace(String instr, String str1, String str2 ){

String outstr;
StringBuffer strB = new StringBuffer();

StringTokenizer strT = new StringTokenizer(instr, str1, false);
while(strT.hasMoreTokens())
{
strB.append(strT.nextToken()).append(str2);
}
return strB.toString();
}
The function work fine replacing CR with <br>. I would also use the function to replace whitespace with %20. I have tried to call the function with:
myHTML=replace(myHTML,&quot; &quot;,&quot;%20&quot;);

The problem is that when i try to replace multiple whitespaces, they are onlye replaced with one %20.

Like &quot;test *&quot; is convert to &quot;test%20*&quot;, i would it to be convert to &quot;test%20%20%20%20%20%20*&quot;.

Anybody that know how i can do this?

Petal,Norway
 
Try this:
Code:
public String replaceChar(String input, char old, String new) {
  StringBuffer result = new StringBuffer();
  char c;
  for (int i=0; i<input.length(); i++) {
     c = input.charAt(i);
     if (c == old) 
        result.append(new);
     else
        result.append(c);
  }
  return result.toString();
}

This should work but I make no guarantees because I didn't actually test it. Wushutwist
 
Thanks wushutwist
This tips where very helpufull!

Petal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top