Replace method in stringbuffer

C

chris1980

OMG. This is like my worst nightmare. i wanted to replace all "*" with
"occupant". like for instance: by doing alot of research i still cannot
get a good description on stirngbuffer replace method. here is my
program for the time being. i wanted to keep it as simple.
class MailGenerator {
public static void main(String[] args) {
String st = ("I have * exciting * news for you, *!!!. For just $49.99
plus postage");
StringBuffer sb = new StringBuffer(st);
sb.replaceAll(st, "*", "Occupant");
System.out.println(sb);
}
}
i want to replace * by occupant. i cant seem to rite replaceall method.
i looked at the api it asks for the starting index and ending index. i
can't make any sense of it. appreciate any help.
 
J

Jean-Francois Briere

There is no StringBuffer.replaceAll() method.
There is a String.replaceAll(String regex, String replacement) method
though that uses regular expression and is simple to use.
If you absolutely need to replace the chars inside a StringBuffer then
you should create your own replaceAll() method.
You should use SringBuffer.indexOf(String str, int from) and
SringBuffer.replace(int start, int end, String str) methods in a loop
to implement it.

Regards
 
C

chris1980

Paul said:
Use String's replaceAll instead :
st = st.replaceAll( "\\*", "Occupant");

Paul thanks alot. i know i can easily use the string.replaceAll method
but since i am new to this stringbuffer i would like in which i have
been successful.
 
C

chris1980

Jean-Francois Briere said:
There is no StringBuffer.replaceAll() method.
There is a String.replaceAll(String regex, String replacement) method
though that uses regular expression and is simple to use.
If you absolutely need to replace the chars inside a StringBuffer then
you should create your own replaceAll() method.
You should use SringBuffer.indexOf(String str, int from) and
SringBuffer.replace(int start, int end, String str) methods in a loop
to implement it.

Regards

This is what i really wanted to do. but for some reasons due to my lack
of knowledge with this stringbuffer i can't make a method and when i do
make a method it gives me error. what is my start, what is my end?
those questions still remains unanswered. i would like an example with
some comments.
Thanks
 
P

Patricia Shanahan

chris1980 said:
This is what i really wanted to do. but for some reasons due to my lack
of knowledge with this stringbuffer i can't make a method and when i do
make a method it gives me error. what is my start, what is my end?
those questions still remains unanswered. i would like an example with
some comments.
Thanks

Is the problem lack of access to the API documentation, or inability to
apply it? In particular, have you read the API documentation for both
replace and indexOf?

Patricia
 
C

chris1980

after hours of debugging i had an updated code.

class Tester {
public StringBuffer replace (StringBuffer str, String searchString,
String
newString)
{
StringBuffer buffer = new StringBuffer(str.toString());
int start = buffer.toString().indexOf(searchString);
int end = start+searchString.length();
return buffer.replace(start, end, newString);


}



}
class MailGenerator {
public static void main(String[] args) {
Tester test = new Tester();
String st = ("I have exciting * news for you, *!!!. For just $49.99
plus postage");
System.out.println("Original String " +st) ;
StringBuffer sb = new StringBuffer(st);
System.out.println("replace string " +test.replace(sb,"*",
"Occupant"));

}
}
Now the problem is it is just removing the first "*" not the second
"*". i know i need to use a for loop so this is what i did.
for(int i=0; i<sb.length(); i++)
System.out.println("replace string " +test.replace(sb,"*",
"Occupant"));
Still giving me the same output.
 
J

Jean-Francois Briere

Almost there. All you have to do now is to put this logic into a loop.
It could be something like that:

public StringBuffer replace(StringBuffer str, String searchString,
String newString)
{
StringBuffer buffer = new StringBuffer(str.toString());
int start = 0;
while ((start = buffer.indexOf(searchString, start)) != -1)
{
...
}
return buffer;
}

By the way, why do you create a new StringBuffer instead of modifying
the one passed as parameter (str)?

Regards
 
C

chris1980

Jean-Francois Briere said:
Almost there. All you have to do now is to put this logic into a loop.
It could be something like that:

public StringBuffer replace(StringBuffer str, String searchString,
String newString)
{
StringBuffer buffer = new StringBuffer(str.toString());
int start = 0;
while ((start = buffer.indexOf(searchString, start)) != -1)
{
...
}
return buffer;
}

By the way, why do you create a new StringBuffer instead of modifying
the one passed as parameter (str)?

Regards

Hmm very true. i guess i was going fast and trying to complete this
exercise. btw this is not my assignment. just curious to see how
stringbuffer things works, Thanks for the hint. actually i did applied
the while loop in my method which was something like this.
int count=0;
while(!count=-1)
buffer.replace(start, end, newstring)
but my output was not rite. thanks alot Jean.
 
J

John Ersatznom

chris1980 said:
OMG. This is like my worst nightmare.
class MailGenerator {
public static void main(String[] args) {
String st = ("I have * exciting * news for you, *!!!. For just $49.99

This is like my worst nightmare too. A recurring one, at that. In fact
it happened again just now. I was at my b0x and I must have dozed off,
because next thing I know I was there again ... everything slow as
molasses, you know the feeling like you can try to run but not make any
progress, and there was no escape from the zillion form-generated
punctuation-laden messages in my inbox offering me stuff for $49.99 and
we'll throw in lots of spare exclamation points FREE! I couldn't delete
them as fast as new ones came in ...

Oh wait, that wasn't a dream, that was me checking my mail, nevermind.

Still, I have to wonder why this newsgroup's other participants are
helping develop what appears to be spamming software.
 
C

chris1980

oh man. you got me really wrong. i am not creating a spamming software
or anything lol i dont even know how to make one. dude the class name
(mailgenerator) i just creATED. it has nothing to do with spamming or
anything lol. i did'nt even realized about that thing. please dont sue
me i am not a spammer nor a hacker i am an ordinary programmer and a
database developer :)
 
A

Andrew Thompson

chris1980 said:
oh man. you got me really wrong. i am not creating a spamming software
or anything lol i dont even know how to make one. dude the class name
(mailgenerator) i just creATED. it has nothing to do with spamming or
anything lol. i did'nt even realized about that thing. please dont sue
me i am not a spammer nor a hacker i am an ordinary programmer and a
database developer :)

<translated to a language even Andrew can understand>
Dude. The client gave me the email addresses when I
lied and told them I could do this (before crawling in here
to try to get others to do it for me) - what do I care where
the addresses came from?

...and can you send me the damn codes, already?
</translated to a language even Andrew can understand>

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top