Solved

How to trim this?

Posted on 2006-07-17
12
285 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 32 total points
Comment Utility
<%
String username1 = (String)session.getAttribute("j_username");
username1 = username1.trim();

%>
0
 
LVL 8

Expert Comment

by:kaliyugkaarjun
Comment Utility

<%
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 31 total points
Comment Utility
>> username1.trim(); //now username contains  string without trailing space

No it doesn't.  String is immutable

owenli27 is right
0
 
LVL 11

Assisted Solution

by:Manish
Manish earned 31 total points
Comment Utility
<%
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 31 total points
Comment Utility
<%
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
I agree :-)

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

Cheers,

Tim
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now