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.


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

Results 1 to 3 of 3

Thread: How to remove the in-between spaces in a String? Please help.

  1. #1
    Junior Member marksquall's Avatar
    Join Date
    Jan 2009
    My Mood
    Thanked 1 Time in 1 Post

    Question How to remove the in-between spaces in a String? Please help.

    Dear javaprogrammingforum.com members and administrators,

    A greeting of peace. I hope and pray that every one is doing fine upon reading this forum.

    Well, I have a problem regarding String input. I already have a program that enters a name and the program will count the total letters used. But unfortunaely, if in case the user enters his/her whole name, of course he/she will enter spaces in between names, the program will still count the letters, but plus the spaces. I don't know to how remove those spaces in the String literal so that I may overwrite, or store in another String, object.

    Thank you for those who will give me some tips regarding this String manipulation.

    Respectfully Yours,

    Mark Squall

  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Warrington, United Kingdom
    My Mood
    Thanked 156 Times in 152 Posts

    Default Re: How to remove the in-between spaces in a String? Please help.


    String replaced = "test1 test2 test3 test4".replaceAll("\\s");

    // Json

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: How to remove the in-between spaces in a String? Please help.

    If you only want letters to be counted (say, ignore numbers, spaces, and other characters) test to see if the character is a letter before adding it to the list:

    for (int i = 0; i < str.length; i++)
         if (Character.isLetter(str.charAt(i))

Similar Threads

  1. [SOLVED] How to Put Spaces in the Output of Passed Arguments?
    By EmSaint in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 04:04 PM
  2. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  3. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM
  4. Remove from array after a period of time
    By Marty in forum Collections and Generics
    Replies: 1
    Last Post: September 1st, 2009, 07:57 AM