Solved

How to trim this?

Posted on 2006-07-17
12
289 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
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 31 total points
ID: 17131236
>> username1.trim(); //now username contains  string without trailing space

No it doesn't.  String is immutable

owenli27 is right
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 11

Assisted Solution

by:Manish
Manish earned 31 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 31 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
org.apache.jasper.JasperException: using jstl in jsp embedded jetty 13 653
countXY challenge 28 160
servlet example 17 51
jsp insert to database example 2 43
Most MSPs worth their salt are already offering cybersecurity to their customers. But cybersecurity as a service is wide encompassing and can mean many things.  So where are MSPs falling in this spectrum?
The advancement in technology has been a great source of betterment and empowerment for the human race, Nevertheless, this is not to say that technology doesn’t have any problems. We are bombarded with constant distractions, whether as an overload o…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

792 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