• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trimming a blank String  RSS feed

 
Barry Sanders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To determine if the String is blank, should it not be if(value != null && value.trim().length() == 0) ?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No quotes.

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

I wonder if this is the difference.
 
Barry Sanders
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just verified - it is because it was   instead of just " "
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you try this?

 
john cahil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!