How do I remove extra characters (spaces) at the end of a string?

Posted on 2007-10-02
Last Modified: 2013-11-23
Pretty much as question implies.

I have a string which may have spaces at the end of it.  I want to check if it does and remove them if so.
Question by:Lamont77
    1 Comment
    LVL 16

    Accepted Solution

    The trim function will remove any leading or trailing spaces from a String.

    String mystring="This is a string     ";

    String cleanstring=mystring.trim();

    Now cleanstring will be "This is a string".

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    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…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    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 …

    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

    18 Experts available now in Live!

    Get 1:1 Help Now