Results 1 to 7 of 7
  1. #1
    andy9507 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default Removing All Commas from a String

    How can I remove all instances of a comma from a string?

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    There's an extremely easy and quick way, but I won't spell it out for you. Read through String (Java Platform SE 6) and see if you can find it!

  3. #3
    andy9507 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    I made my own method that I edited for my purpose from someone else, thank you for the link though!

    import java.util.StringTokenizer;
    public class CharacterRemover {
    public String removeCharacter(String s, String removeChar) {
    StringTokenizer st = new StringTokenizer(s,removeChar,false);
    String t="";
    while (st.hasMoreElements()) t += st.nextElement();
    return t;
    }
    }

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by andy9507 View Post
    I made my own method that I edited for my purpose from someone else, thank you for the link though!

    import java.util.StringTokenizer;
    public class CharacterRemover {
    public String removeCharacter(String s, String removeChar) {
    StringTokenizer st = new StringTokenizer(s,removeChar,false);
    String t="";
    while (st.hasMoreElements()) t += st.nextElement();
    return t;
    }
    }
    Or you can do that with one line:

    Java Code:
    String newStr = oldStr.replaceAll(",", "");
    Last edited by Solarsonic; 05-09-2011 at 10:59 PM.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You could also use split, you should try to avoid string tokenizer.

  6. #6
    andy9507 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks guys, and yeah, I found that replaceAll method later, I'll be sure to use that next time to save me the trouble.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,239
    Rep Power
    19

    Default

    Just remember that replaceAll(...) takes a regex argument. For simple cases where you don't require the matching flexibility of a regular expression, use replace(...).

    db

Similar Threads

  1. Getting rid of commas in large numbers?
    By wwuster in forum Advanced Java
    Replies: 12
    Last Post: 03-05-2012, 10:35 AM
  2. Removing words from a String
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-15-2011, 10:50 AM
  3. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 12:43 AM
  4. Replies: 1
    Last Post: 02-20-2009, 02:06 PM
  5. Replies: 1
    Last Post: 01-11-2008, 10:06 PM

Posting Permissions

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