Find the answer to your Linux question:
Results 1 to 4 of 4
Hey everyone, I am writing a program that prints out lines of information, and different lines need different indentations. So I have one string that i add whitespace to increase ...
  1. #1
    Just Joined!
    Join Date
    Aug 2006
    Posts
    59

    Question how do i remove a specific amount of whitespace within a string???(Java)

    Hey everyone,

    I am writing a program that prints out lines of information, and different lines need different indentations. So I have one string that i add whitespace to increase the indentation...

    For example:
    Code:
    String indentation = "";
    -And when i want to increase the indentation(two spaces) i just do the following:
    Code:
    indentation += "  ";
    This works, however i want to then be able to substract whitespace from the indentation string, and i am not quite sure how to do this...

    Thanks to whomever can help,
    Tom

  2. #2
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    To remove all whitespace characters from the beginning and end of a string, use the trim() method. For instance, say we have a string instance called "spacesAndString":

    Code:
    String noSpaceString = spacesAndString.trim();
    .trim() returns the string minus whitespace characters and puts it in your destination variable.

    For more information, check the Sun Java documentation for String:

    http://java.sun.com/j2se/1.4.2/docs/api/index.html
    Registered Linux user #270181
    TechieMoe's Tech Rants

  3. #3
    Linux Newbie birdman's Avatar
    Join Date
    Mar 2006
    Location
    Ireland
    Posts
    141
    I think you should use the substring method. By using

    Code:
    if(indentation.length() >= 2){
        indentation = indentation.substring(2);
    }
    You should be able to step up and down the indentation steps quite easily.

    Regards

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    If you keep the indentation as an integer, then operations like this are probably easier. You can then set the amount of an indent separately, say, 2 spaces, 3 spaces, etc., and write the total number of spaces as "n" chunks, where "n" is the current value of indentation. This is, however, a change in how you view the problem ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...