Solved

How to trim this?

Posted on 2006-07-17
12
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iterator example remove 8 75
instanceof  operator in java 26 114
batch vs regular insert in spring DAO 2 52
JVM error from eclipse 1 27
This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

752 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