Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

Replace method in stringbuffer

Thread view: 
chris1980 - 15 Jan 2007 06:26 GMT
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.
Paul Hamaker - 15 Jan 2007 08:21 GMT
Use String's replaceAll instead :
st = st.replaceAll( "\\*", "Occupant");
--
http://javalessons.com  Paul Hamaker, SEMM
Teaching ICT since 1987
chris1980 - 15 Jan 2007 08:54 GMT
> Use String's replaceAll instead :
>  st = st.replaceAll( "\\*", "Occupant");
> --
> http://javalessons.com  Paul Hamaker, SEMM
> Teaching ICT since 1987

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.
Jean-Francois Briere - 15 Jan 2007 08:21 GMT
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
chris1980 - 15 Jan 2007 08:56 GMT
> There is no StringBuffer.replaceAll() method.
> There is a String.replaceAll(String regex, String replacement) method
[quoted text clipped - 6 lines]
>
> 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
Andrew Thompson - 15 Jan 2007 09:29 GMT
...
> ...i would like an example with some comments.

I would like a pony.

Andrew T.
Patricia Shanahan - 15 Jan 2007 09:43 GMT
>> There is no StringBuffer.replaceAll() method.
>> There is a String.replaceAll(String regex, String replacement) method
[quoted text clipped - 13 lines]
> 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
chris1980 - 15 Jan 2007 20:48 GMT
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.
Jean-Francois Briere - 15 Jan 2007 21:25 GMT
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
chris1980 - 15 Jan 2007 21:39 GMT
> Almost there. All you have to do now is to put this logic into a loop.
> It could be something like that:
[quoted text clipped - 15 lines]
>
> 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.
John Ersatznom - 16 Jan 2007 15:05 GMT
> 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.
chris1980 - 18 Jan 2007 05:08 GMT
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 :)
Andrew Thompson - 18 Jan 2007 05:24 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.