Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to trim this?

Posted on 2006-07-17
12
Medium Priority
?
294 Views
Last Modified: 2010-04-01
How can I trim this string?  
It has a space at the end.

<%
String username1 = (String)session.getAttribute("j_username");
%>
0
Comment
Question by:g118481
12 Comments
 
LVL 9

Accepted Solution

by:
owenli27 earned 128 total points
ID: 17127189
<%
String username1 = (String)session.getAttribute("j_username");
username1 = username1.trim();

%>
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
ID: 17127851

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

%>
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 124 total points
ID: 17131236
>> username1.trim(); //now username contains  string without trailing space

No it doesn't.  String is immutable

owenli27 is right
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Assisted Solution

by:Manish
Manish earned 124 total points
ID: 17137114
<%
String username1 = (String)session.getAttribute("j_username");
// check for null otherwise it will throw null pointer exception
if(username1!=null){
username1 = username1.trim();
}

%>
0
 
LVL 5

Assisted Solution

by:muktajindal
muktajindal earned 124 total points
ID: 17173233
<%
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
 
LVL 35

Expert Comment

by:TimYates
ID: 17173751
muktajindal, your first suggestion is not as elegant as karanw's (imho)

and your second one gives me the fear ;-)
0
 
LVL 5

Expert Comment

by:muktajindal
ID: 17173793
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
 
LVL 35

Expert Comment

by:TimYates
ID: 17173805
>> 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
 
LVL 5

Expert Comment

by:muktajindal
ID: 17173942
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
 
LVL 35

Expert Comment

by:TimYates
ID: 17173951
I agree :-)

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

Cheers,

Tim
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
How to fix a SonicWall Gateway Anti-Virus firewall blocking automatic updates to apps like Windows, Adobe, Symantec, etc.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Integration Management Part 2
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question