473,327 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

String replace()

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
3 17776
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.