473,796 Members | 2,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
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(te mp,'_');
}
}
}
return hiddenWord;
}

Any help much appreciated! Thanks :)
Jul 17 '05 #1
3 17815
ne************* @hotmail.com (- ions) wrote in message news:<47******* *************** ****@posting.go ogle.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 = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
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(te mp,'_');
}
}
}
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.go ogle.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 = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
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(te mp,'_');
}
}
}
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(te mp,'_');

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.repl ace(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.go ogle.com>...

public String hideWord(String word)
{
String alphabet = "ABCDEFGHIJKLMN OPQRSTUVWXYZ";
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(te mp,'_');
change this to:
hiddenWord=hidd enWord.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
62116
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 supposed to write this function? String.replace(/</g,'&lt;');
13
11026
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 below would work... string gsub(const string & sData, const string & sFrom,
13
2366
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
21711
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
14903
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 ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
12
9646
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(" & Chr(34) & PopUpWindowTitle & Chr(34) & ", " & Chr(34) & CurrentEventDetails & ")" strTemp += "<BR><A HREF='#' onClick='" & PopupLink & "'>" & EventName & "</A><BR>" The problem I have is that when the string variables or contain a string with an...
9
2157
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, Y=156}{X=311.2285, Y=156}{X=356, Y=200.7715}{X=356, Y=256}{X=200, Y=150}{X=200, Y=177.6142}{X=177.6142, Y=200}{X=150, Y=200}{X=122.3858, Y=200}{X=100, Y=177.6142}{X=100, Y=150}{X=100, Y=122.3858}{X=122.3858, Y=100}{X=150, Y=100}{X=177.6142, Y=100}{X=200,...
9
9161
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 StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
15
50264
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
3281
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 trying to sort them by the date modified field. So what's the problem, right? Well, obviously Sep 14, 2008 is before Nov 14, 2008, but when doing a sort process, ActionScript will come back with Nov coming before Sep, because N comes before S in...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.