By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,740 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

String replace()

P: n/a
Hi, i would like to know how to replace every char in a string with a
certin given char using the String.replace(char oldChar,char newChar).
I would like to replace all letters with an underscore ie. "hello
world" will be come...
"_ _ _ _ _ _ _ _ _ _" (ive added xtra spaces so it dosnt look like
one line!) with the method i have written only the last char is
replaced, loop problem i guess!

public String hideWord(String word)
{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String hiddenWord="";

for(int i = 0;i<word.length();i++)
{
char temp = word.charAt(i);

for(int j=0;j<alphabet.length();j++)
{
char letters = alphabet.charAt(j);

if(temp == letters)
{
hiddenWord = word.replace(temp,'_');
}
}
}
return hiddenWord;
}

Any help much appreciated! Thanks :)
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ne*************@hotmail.com (- ions) wrote in message news:<47**************************@posting.google. com>...
Hi, i would like to know how to replace every char in a string with a
certin given char using the String.replace(char oldChar,char newChar).
I would like to replace all letters with an underscore ie. "hello
world" will be come...
"_ _ _ _ _ _ _ _ _ _" (ive added xtra spaces so it dosnt look like
one line!) with the method i have written only the last char is
replaced, loop problem i guess!

public String hideWord(String word)
{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String hiddenWord="";

for(int i = 0;i<word.length();i++)
{
char temp = word.charAt(i);

for(int j=0;j<alphabet.length();j++)
{
char letters = alphabet.charAt(j);

if(temp == letters)
{
hiddenWord = word.replace(temp,'_');
}
}
}
return hiddenWord;
}

Any help much appreciated! Thanks :)

Expand|Select|Wrap|Line Numbers
  1. word.replaceAll("[^ ]", "_"); //preserve spaces
  2.  
Jul 17 '05 #2

P: n/a
ne*************@hotmail.com (- ions) wrote in message news:<47**************************@posting.google. com>...
Hi, i would like to know how to replace every char in a string with a
certin given char using the String.replace(char oldChar,char newChar).
I would like to replace all letters with an underscore ie. "hello
world" will be come...
"_ _ _ _ _ _ _ _ _ _" (ive added xtra spaces so it dosnt look like
one line!) with the method i have written only the last char is
replaced, loop problem i guess!

public String hideWord(String word)
{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String hiddenWord="";

for(int i = 0;i<word.length();i++)
{
char temp = word.charAt(i);

for(int j=0;j<alphabet.length();j++)
{
char letters = alphabet.charAt(j);

if(temp == letters)
{
hiddenWord = word.replace(temp,'_');
}
}
}
return hiddenWord;
}


While hiwa's solution is the much better one, I can point out the
error in
your code. It is this line:

hiddenWord = word.replace(temp,'_');

On each iteration you are assigning hiddeWord the value of word after
it has had a single letter replaced. word, itself, remains unchanged
from loop to loop, to the changes are not cumultive.

hiddenWord = hiddenWord.replace(temp, '_');

is what you want. Of course, it assumes that above the loops you have
initially assign hiddenWord to word as in

hiddenWord = word;

Greg

PS: hiwa's solution is definitely better!
Jul 17 '05 #3

P: n/a
ne*************@hotmail.com (- ions) wrote in message news:<47**************************@posting.google. com>...

public String hideWord(String word)
{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String hiddenWord="";
change this to:
hiddenWord=word;

for(int i = 0;i<word.length();i++)
{
char temp = word.charAt(i);

for(int j=0;j<alphabet.length();j++)
{
char letters = alphabet.charAt(j);

if(temp == letters)
{
hiddenWord = word.replace(temp,'_');
change this to:
hiddenWord=hiddenWord.replace(temp,'_');

this will use the result of the previous 'replace()' as the input
to the next one. remember strings are immutable.

this seems a bit of a roundabout way of doing it, but yes it
should work...
}
}
}
return hiddenWord;
}

Any help much appreciated! Thanks :)

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.