Removing spaces from a Java string

Posted on 2007-10-16
Last Modified: 2013-11-23
I have a simple java program reading a name of type string from the database.
If the string contains 2 spaces between the first name and the second name I want to remove one of these spaces. Example if the database contains the name David  Jones (2 spaces between David and Jones) I want to return David Jones(only 1 spaces between David and Jones).

Seems simple but confusing me none the less??
Thanks in advance
Question by:mr_kirbs
    LVL 92

    Accepted Solution

    try something like:

    s = s.replaceAll("  +", " ");

    Author Comment

    What I used:

    .replaceAll("  ", " ");


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Top Freelancers to Complete Java Projects

    Source the talented Expert Exchange community
    for top quality work on your Java projects.

    Hire the best. Collaborate easily. Get quality work.

    Suggested Solutions

    Title # Comments Views Activity
    EvenOdd challenge 10 68
    Issues installing SSL certificate into Apache Tomcat 3 48
    stringclean challenge 26 43
    word0 challenge 3 30
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now