6 Replies Latest reply: Jan 29, 2008 8:06 PM by 807601 RSS

    Remove Spaces from String

    807601
      Hello every one, I'm a student in hight school taking object oriented programing.
      I've been ahead of my class so far so the teacher has given me an extra thing to add to out current problem.

      We have to create a program that:
      1: Takes First name, and Last name
      2: Counts the letters in each
      3: Display them both and then add them.
      *4: (This is the one the teacher suggests I try to do) have it not count spaces entered in to the dialog box.

      Now So far I've got the basic assignment covered. I'm interested in solving number 4 however to impress the teacher.

      <edit> Oop forgot the question, hehe.
      Is there any method that either removes spaces from a string or counts the string without spaces?

      I've got the code if its needed, If any one asks I'll add it.

      Edited by: Voot_Lejin on Jan 29, 2008 3:44 PM
        • 1. Re: Remove Spaces from String
          807601
          You can use String.trim() for this. Here is an example:

          class Trim
          {
               public static void main (String args [])
               {
                    String st = " Sample String ";
                    System.out.println("<" + st + ">");
                    System.out.println("<" + st.trim() + ">");
                    
               }
          }
          • 2. Re: Remove Spaces from String
            807601
            Oh, ok, I checked out the idea of String.trim(); before, the API just says it takes out the white space Around the string. SO my next question is, does it remove it from the middle of the string? Here's an example:

            Input: Voot
            input: Lejin

            Output: Your first name is: Voot it has 4 letters in it.
            Your last name is: Lejin it has 5 letters in it.

            Now here is the problem:
            Input: Vo ot
            input: Leji n

            output: your first name is: Vo ot it has 5 letters in it.
            Your last name is: Leji n it has 6 letters in it.

            Now I want it to remove the Spaces, or at least not count them.
            Thanks for your help so far As well :)
            • 3. Re: Remove Spaces from String
              807601
              The trim() method is not going to work for your purpose. As this is an exercise that you want to impress your teacher with, we should leave at least some of the effort for you. ;-)

              I'll give you two hints though:
              Number one: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char)
              Number two: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

              Here's some instructions as well: Create a method that iterates over the characters contained in your String and add only those characters to the StringBuffer that are not whitespace characters. Finally return the StringBuffer's String representation.
              • 4. Re: Remove Spaces from String
                807601
                steinwachs wrote:
                The trim() method is not going to work for your purpose. As this is an exercise that you want to impress your teacher with, we should leave at least some of the effort for you. ;-)

                I'll give you two hints though:
                Number one: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Character.html#isWhitespace(char)
                Number two: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

                Ok, here's some instructions as well: Create a method that iterates over the characters contained in your String and add only those characters to the StringBuffer that are not whitespace characters. Finally return the StringBuffer's String representation.
                It would be even easier with a regular expression.

                http://java.sun.com/docs/books/tutorial/essential/regex/pre_char_classes.html
                http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
                • 5. Re: Remove Spaces from String
                  807601
                  To remove all spaces, check out String.replace()
                  • 6. Re: Remove Spaces from String
                    807601
                    CaptainMorgan08 wrote:
                    It would be even easier with a regular expression.>
                    Easier (shorter), if he knows about regular expressions, and another good exercise for the questioner. The StringBuffer version is significantly faster though, at least for this simple purpose.