String trim spaces...

Posted on 2004-10-21
Last Modified: 2010-03-31
Is there a function that will let me get rid of all spaces in a String. I know trim() only get rid of spaces in front and end. If there is no function what would be the quickest way to get rid of all spaces in a string.

Thanks experts
Question by:apparition
    LVL 37

    Expert Comment

      yourString.replaceAll("[ ]", "")
    LVL 37

    Accepted Solution

       yourString.replaceAll(" ", "")
    LVL 37

    Expert Comment

    These all work

            System.out.println( "   hello  world   ".replaceAll("[ ]", ""));
            System.out.println( "   hello  world   ".replaceAll("[ ]+", ""));
            System.out.println( "   hello  world   ".replaceAll(" ", ""));
    LVL 37

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One Java Help from Top Experts

    Solve your toughest problems, fast.
    Java experts are online now and ready to help you.

    Suggested Solutions

    Title # Comments Views Activity
    Spring Boot Security, adding filters to different routes 2 25
    dividesSelf challange 15 38
    IT Company 5 35
    json format text only 4 22
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now