Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 29th September 2002, 3:58 PM   #1
TruPlaya Thread Starter
Member
 
Join Date: Jul 2002
Location: Sydney
Posts: 466
Default Java String Help!!

Rather simple.

I have this string:

A, b747 ||), (C, a300 -- --), (Y, a300 -- ||

I need to replace in it, "), (" with "\n" but I can't find anything that can.

Ive tried String.replace('(', '\n');

but then you can't replace ' ' & '(' with ''.

Ive looked at:
public String replaceAll(String regex, String replacement)
but that doesnt seem to help me because...

ns = ns.replaceAll("), (", "\n");
it compiles fine, but throws this...
java.util.regex.PatternSyntaxException: Unmatched closing ')'
TruPlaya is offline   Reply With Quote

Join OCAU to remove this ad!
Old 29th September 2002, 4:28 PM   #2
Geo
Member
 
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
Default

You need to use regular expression syntax to use that replaceAll method. There should be something about it in the java docs.

Failing that you could do it using indexOf, substring and a StringBuffer.
__________________
"That's the way good software gets designed. So if you pull out a piece it won't run" - Steve Ballmer
Geo is offline   Reply With Quote
Old 29th September 2002, 5:12 PM   #3
Bangers
Member
 
Bangers's Avatar
 
Join Date: Dec 2001
Location: Silicon Valley
Posts: 7,281
Default

Code:
ns = ns.replaceAll("\\), \\(", "\n");
__________________
There's a story about a golfer who sinks a 30-meter putt and someone says: 'Gee, that was lucky' and the golfer says, 'Yes, amazing how lucky you get when you practice 8 hours a day for 20 years'.
Bangers is offline   Reply With Quote
Old 30th September 2002, 1:53 PM   #4
snoopie
Member
 
snoopie's Avatar
 
Join Date: Aug 2001
Location: Sydney
Posts: 1,152
Default

Quote:
Originally posted by Geo
Failing that you could do it using indexOf, substring and a StringBuffer.
Like this
Code:
    public static String replaceSubstring(String str, String sub, String with) {
        int length = sub.length();
        for (int i=str.indexOf(sub); i!=-1; i=str.indexOf(sub)) {
            StringBuffer strbuf = new StringBuffer(str).replace(i, i+length, with);
            str = strbuf.toString();
        }
        return (str);
    }
Edit: Ah... i was wondering why i would write my own method when there was already a replaceAll, its cause replaceAll isn't in JDK1.3 only 1.4+
__________________
Don't use a big word where a diminutive one will suffice.

Traders List....
(circa September 2003)

Last edited by snoopie; 30th September 2002 at 2:07 PM.
snoopie is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 9:38 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!