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 ...
- 04-10-2007 #1Just Joined!
- Join Date
- Aug 2006
- Posts
- 59
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:
-And when i want to increase the indentation(two spaces) i just do the following:Code:String 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...Code:indentation += " ";
Thanks to whomever can help,
Tom
- 04-10-2007 #2
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":
.trim() returns the string minus whitespace characters and puts it in your destination variable.Code:String noSpaceString = spacesAndString.trim();
For more information, check the Sun Java documentation for String:
http://java.sun.com/j2se/1.4.2/docs/api/index.htmlRegistered Linux user #270181
TechieMoe's Tech Rants
- 04-10-2007 #3
I think you should use the substring method. By using
You should be able to step up and down the indentation steps quite easily.Code:if(indentation.length() >= 2){ indentation = indentation.substring(2); }
Regards
- 04-11-2007 #4Linux Engineer
- 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, drlWelcome - 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 )


Reply With Quote