Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-16-2008, 04:45 PM
Member
 
Join Date: Jan 2008
Posts: 2
hamish10101 is on a distinguished road
Find and replace ( in a String
hi,

Can anyone please help me find and replace character ( in a string.
For example String is abc(xyz
how can i obtain the string abcxyz?

I tried the following code but it doesn work

Pattern pattern = Pattern.compile("(");
Matcher matcher = pattern.matcher("abc(xyz");
String output = matcher.replaceAll("");

I get the error
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1
(
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.accept(Unknown Source)
at java.util.regex.Pattern.group0(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at Tester.main(Tester.java:51)

Kindly point out my mistake or suggest an alternative.

Thanks,
Hamish
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-16-2008, 05:48 PM
Member
 
Join Date: Dec 2007
Location: KSA, Riyadh
Posts: 9
AralX is on a distinguished road
I have an Idea , which is a very simple ..
----------------------------------------
String str = "abc(xyz",newStr="";
for(int x=0; x<str.length();x++)
if(str.charAt(x)>='a' ||str.charAt(x)>='z' )//a ssuming they are all lower cases
newStr =""newStr+str.charAt(x);

-------------------------------------------------

I hope that helped - this code will only print letters while ..this -below- will not print this pranthes

----------------------------------------
String str = "abc(xyz",newStr="";
for(int x=0; x<str.length();x++)
if(str.charAt(x)!='(' )

newStr =""newStr+str.charAt(x);

-------------------------------------------------
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-16-2008, 11:31 PM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
There is a method in String, replace(CharSequence target, CharSequence replacement)

So, you could do

Code:
String newString = oldString.replace("(", "");
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-16-2008, 11:39 PM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 251
roots is on a distinguished road
Code:
System.out.println("abc(xyz".replace("(", ""));
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-17-2008, 06:42 AM
Member
 
Join Date: Jan 2008
Posts: 2
hamish10101 is on a distinguished road
Thanks
Hey thanks mate
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-17-2008, 06:43 AM
roots's Avatar
Moderator
 
Join Date: Jan 2008
Location: Dallas
Posts: 251
roots is on a distinguished road
No problem hamish .. gibsonrocker800 and i have same answer ... lol
__________________
dont worry newbie, we got you covered.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 01-17-2008, 06:51 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Quote:
Originally Posted by roots View Post
No problem hamish .. gibsonrocker800 and i have same answer ... lol
Hahah yea. When you posted your code i was like. Ah he thinks hes so cool because his code worked with one line of code. Mine required the extra System.out.println() hahaha. =]
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Replace in JTable look4 AWT / Swing 2 11-24-2007 01:27 AM
Using a replace method... paul New To Java 2 08-07-2007 05:50 AM
problems with replace method cecily New To Java 1 08-02-2007 10:11 PM
String replace method venkata.tarigopula Advanced Java 1 07-10-2007 09:14 PM
Using Find And Replace In Eclipse IDE JavaForums Eclipse 0 04-26-2007 11:15 AM


All times are GMT +3. The time now is 12:38 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org