aspose file tools
The moose likes Beginning Java and the fly likes Trimming a blank String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Trimming a blank String" Watch "Trimming a blank String" New topic
Author

Trimming a blank String

Barry Sanders
Greenhorn

Joined: Jul 27, 2006
Posts: 10
This sounds so super easy but this is really bugging me.

I get a text field from my JSP that someone has entered " ".

A single space it appears will not be trimmed down to "" (i.e. length = 0)

Here is my dilemma - how do I trim down a sting that only contains spaces?

So if someone for some reason enters " ". My code still works to say hey this is a blank.

It's kind of weird because everywhere in our app to determine if something is blank we code:

if(value != null && value.trim().length() > 0)

So now I see that the value.trim().length() is useless but I am just running into this problem now.

HEPL
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
To determine if the String is blank, should it not be if(value != null && value.trim().length() == 0) ?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
I tried this sample code, and the trim method did reduce a single space.



Is it possible that the String contains invisible characters like \r or \n.

You might try the method method replaceAll and use the regular expression \\s to try to match any white space as opposed to just a space.
Barry Sanders
Greenhorn

Joined: Jul 27, 2006
Posts: 10
This is exactly what I was saying.

if(value != null && value.trim().length() == 0)

if value is " " three blanks.

Trim does nothing but return " ".

So in this case value.trim().length() is equal to 3 not 0.

Trim only trims space off it determines there is something other than a space in the string, otherwise it just returns the original string which is three blank spaces
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
I think I may have misunderstood your question.

Is the user entering the quotes as well?

In that case, trim() won't do anything.

But you could use a regular expression to remove the " and then use trim().
Barry Sanders
Greenhorn

Joined: Jul 27, 2006
Posts: 10
That's really weird.

This worked - it return 1 0.

String s = " ";
System.out.println(s.length() + " " + s.trim().length());

But this does not...
<INPUT TYP="TEXT" SIZE="10" maxlength="10" VALUE="�"NAME="AdjustAllVolumes" tabindex="3">

My text field filled with 1 blank space.

String volAdjust = (String) request.getParameter("AdjustAllVolumes");

if(volAdjust != null)
System.out.println(volAdjust.length() + " " + volAdjust.trim().length());

When I run the same thing on the String return from that parameter I get 1 1.

Why the difference I wonder?
Barry Sanders
Greenhorn

Joined: Jul 27, 2006
Posts: 10
No quotes.

The text field was oringinally populated with a   when it was created.

I wonder if this is the difference.
Barry Sanders
Greenhorn

Joined: Jul 27, 2006
Posts: 10
just verified - it is because it was   instead of just " "
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2341
What if you try this?

john cahil
Greenhorn

Joined: Jul 26, 2006
Posts: 12
Please Excuse if I missunderstood the question

I think trim is not worknig if we are calling it with " ".

public class Space {

public static void main(String[] args) {
String s=" ";
System.out.print(s.trim().length());
}
}


it is Printing 0.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Trimming a blank String
 
Similar Threads
Cross browser issues
dynamic help
Probs with Sybase
How to create a validate method to validate a string for null, notEmpty and with in specific length
[NX: UrlyBird 1.3.2] Storing data in memory