Click to See Complete Forum and Search --> : Trim() function in JSP


kmonroe6
07-12-2005, 09:24 AM
Hello!

I'm trying to use the trim() function on a session variable, inside a JSP page. I'm trying to do something like:

<%= "Dear " + session.getAttribute("tmpFrstName").trim() +"\nNew line goes here" %>

When I try this, I get:
Error: No method named "trim" was found in type "java/lang/Object".

Any ideas??
Thx

Khalid Ali
07-12-2005, 09:02 PM
yes thats because session.getAttribute() returns an object not a String(which trim is a function of). U should do something like this
String.valueOf(session.getAttribute("tmpFrstName")).trim()
there is one even more important issue. always check for a null value in such cases somethng like this
if(null!=session.getAttribute("tmpFrstName")){
String str = String.valueOf(session.getAttribute("tmpFrstName"));
//and now u apply that trim function
str.trim();
}