How to trim this?

How can I trim this string?  
It has a space at the end.

<%
String username1 = (String)session.getAttribute("j_username");
%>
LVL 1
g118481Asked:
Who is Participating?
 
Weiping DuConnect With a Mentor Senior Web DeveloperCommented:
<%
String username1 = (String)session.getAttribute("j_username");
username1 = username1.trim();

%>
0
 
kaliyugkaarjunCommented:

<%
String username1 = (String)session.getAttribute("j_username");
username1.trim(); //now username contains  string without trailing space

%>
0
 
TimYatesConnect With a Mentor Commented:
>> username1.trim(); //now username contains  string without trailing space

No it doesn't.  String is immutable

owenli27 is right
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ManishConnect With a Mentor LeadCommented:
<%
String username1 = (String)session.getAttribute("j_username");
// check for null otherwise it will throw null pointer exception
if(username1!=null){
username1 = username1.trim();
}

%>
0
 
muktajindalConnect With a Mentor Commented:
<%
String username1 = "";
if (session.getAttribute("j_username") != null)
     username1 = (String)session.getAttribute("j_username");
username1 = username1.trim();
%>

OR if you are sure that it is not null and contains exactly one space at the end, you can do

<%
String username1 = (String)session.getAttribute("j_username");
username1 = username1.substring(0, username1.length()-1);
%>
0
 
TimYatesCommented:
muktajindal, your first suggestion is not as elegant as karanw's (imho)

and your second one gives me the fear ;-)
0
 
muktajindalCommented:
In karanw's solution, he has suggested to check for null in second statement whereas a NullPointerException will be thrown in the first statement itself if session.getAttribute("j_username") is null.

My second solution is for the case when developer is sure that session.getAttribute("j_username") is never null and contains exaclty one space at the end (as he has mentioned in the question " How can I trim this string? It has a space at the end. ")
0
 
TimYatesCommented:
>> whereas a NullPointerException will be thrown in the first statement itself if session.getAttribute("j_username") is null.

No it won't

String s = (String)null ;

is fine

Fair enough about the second bit though :-)  It just worries me that trimming for such a specific case will require updating later when a space occurs at the beginning, or another space appears at the end ;-)
0
 
muktajindalCommented:
Yeah, I'm sorry for the first part. But for the second one, it depends what level of flexibility the developer wants to have in his application. according to his question, its OK.
0
 
TimYatesCommented:
I agree :-)

Sorry for sounding grumpy...  I think I hadn't woken up properly yet :-(

Cheers,

Tim
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.