Results 1 to 7 of 7
  1. #1
    hamish10101 is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    AralX is offline Member
    Join Date
    Dec 2007
    Location
    KSA, Riyadh
    Posts
    9
    Rep Power
    0

    Default

    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);

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

  3. #3
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    There is a method in String, replace(CharSequence target, CharSequence replacement)

    So, you could do

    Java Code:
    String newString = oldString.replace("(", "");

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    System.out.println("abc(xyz".replace("(", ""));
    dont worry newbie, we got you covered.

  5. #5
    hamish10101 is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default Thanks

    Hey thanks mate

  6. #6
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    No problem hamish .. gibsonrocker800 and i have same answer ... lol
    dont worry newbie, we got you covered.

  7. #7
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    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. =]

Similar Threads

  1. Find and Replace in JTable
    By look4 in forum AWT / Swing
    Replies: 2
    Last Post: 11-24-2007, 12:27 AM
  2. Using a replace method...
    By paul in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 04:50 AM
  3. problems with replace method
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 09:11 PM
  4. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM
  5. Using Find And Replace In Eclipse IDE
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •