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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 2 of 2

Thread: Strings and methods

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Strings and methods

    Hello, so I'm working on some code to remove whitespace within String data type variables that hold values such as: " Hello World " and "* Hello World *".
    I know for the first option, you could use the .trim() method. So I messed around and added the '*' character at the front and back, now it doesn't trim the inner whitespace. Could I get an explanation as to why this doesn't work and an alternative to how I could trim the inner whitespace?
    Much appreciated, thank you.

    As for what I've tried so far, I haven't tried anything as of yet as I've just gotten back. I did ponder about it thinking maybe I could combine the .substring() method with .trim(), but as to how to do it. I'm sure. Thanks again.
    P.S. - This is all of the code, it can be simply thrown right into main and do what I'm trying to figure out/where I'm at.

    String myS = "       Hello World!       ";
    System.out.println(myS);
    System.out.println(myS.trim());
    myS = "*       Hello World!       *";
    System.out.println(myS);
    System.out.println(myS.trim());

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Strings and methods

    it doesn't trim the inner whitespace.
    Please read the API doc for the trim method to see what it is supposed to do.
    The API doc is here: http://docs.oracle.com/javase/8/docs/api/index.html

    Look at the API doc for the String class. It has methods that can be used to replace spaces in a String.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Returning methods to main() and other methods
    By jonmackey22 in forum Object Oriented Programming
    Replies: 4
    Last Post: September 25th, 2014, 04:36 PM
  2. Sorting lowercase strings before uppercase strings
    By keepStriving in forum Java Theory & Questions
    Replies: 4
    Last Post: March 26th, 2014, 03:33 PM
  3. [SOLVED] Passing Strings in methods
    By 13hartc in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 16th, 2014, 07:02 PM
  4. Strings
    By NewCoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2014, 04:51 AM
  5. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM

Tags for this Thread