Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Removing punctuation characters from a word [Beginner Java]

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Removing punctuation characters from a word [Beginner Java]

    I have these 2 methods. What I'm currently having troubles is with the cleanWord method. What it can do now is remove only one punctuation either in front or at the back of the string or at both ends. What I'm trying to figure out is how to remove multiple punctuation characters at one go? For example "!!!test" will return "test" after implementing the method. Thank you!

    [but do not clean punctuation found in the middle of the word for example "don't"]
     public static String cleanWord(String word)
        {
            String cleanWord = word;
            int wordLength = cleanWord.length();
     
            if (isPunctuation(cleanWord.charAt(0)) == true) {
                cleanWord = cleanWord.substring(1, wordLength);
                if (isPunctuation(cleanWord.charAt(wordLength - 1)) == true) {
                    cleanWord = word.substring(1, wordLength - 1);
                }
            }
     
            else if (isPunctuation(word.charAt(wordLength - 1)) == true) {
                cleanWord = cleanWord.substring(0, wordLength - 1);
            }    
     
            return cleanWord;
        }
     
     
        public static boolean isPunctuation(Character c) 
        {
            boolean isPunctuation = false;
            String[] punctuationList = PUNCTUATION.split("");
            String d = c.toString();
     
            for (String punctuation : punctuationList) {
                if (d.equals(punctuation)) {
                    isPunctuation = true;
                }
            }
     
            return isPunctuation;
        }
    Last edited by starlest; May 8th, 2013 at 10:12 AM. Reason: Solved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing punctuation characters from a word [Beginner Java]

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    how to remove multiple punctuation characters at one go?
    What does "at one go" mean? Are you trying to write a method that will take a String, remove all punctuation characters and return a new String with them removed.

    Can you post some examples?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing punctuation characters from a word [Beginner Java]

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    What does "at one go" mean? Are you trying to write a method that will take a String, remove all punctuation characters and return a new String with them removed.

    Can you post some examples?
    Yep for example "!test,." will return "test". I'm just having troubles removing multiple punctuation characters before or after the word. Removing single punctuation character is not a problem. Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Removing punctuation characters from a word [Beginner Java]

    If the parts of the String to be saved are contiguous, then the String class's substring method can return it.
    Scan in from either end to find the start/end of the String to be saved.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Removing punctuation characters from a word [Beginner Java]

    Quote Originally Posted by Norm View Post
    If the parts of the String to be saved are contiguous, then the String class's substring method can return it.
    Scan in from either end to find the start/end of the String to be saved.
    Thanks a lot! Solved!

Similar Threads

  1. Replies: 7
    Last Post: March 29th, 2013, 07:38 AM
  2. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  3. Removing Unnecessary Characters from Servlet class
    By charanraj_d in forum Java Servlet
    Replies: 1
    Last Post: November 2nd, 2011, 10:52 AM
  4. (Real beginner) Removing space before the first word when switched
    By skw4712 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 13th, 2011, 06:43 AM
  5. removing stopwords from a word list
    By jessie in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 15th, 2010, 12:02 PM