![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Jul 2002
Location: Sydney
Posts: 466
|
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 ')' |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Jun 2001
Location: Adelaide
Posts: 2,377
|
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 |
|
|
|
|
|
#3 |
|
Member
Join Date: Dec 2001
Location: Silicon Valley
Posts: 7,281
|
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'. |
|
|
|
|
|
#4 | |
|
Member
Join Date: Aug 2001
Location: Sydney
Posts: 1,152
|
Quote:
![]() 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);
}
__________________
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. |
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|