Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

java trim() issue

hi guys

In my code i have

DetailVO obj = detailIterator.next();	
String name = obj.getName();
String trimmedName = obj.getName().trim();
System.out.println("name>>>"+name);  //prints  MICHEL, JEFF
System.out.println("trimmedName>>>"+trimmedName); //prints  MICHEL, JEFF

Open in new window

Any idea why trimmedName doesnt show trimmed spaces?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of CPColin
CPColin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

any idea how i can get a completely trimmed string like >>MICHEL,JEFF
thx
You would have to do something like this:

Construct a StringBuilder
Iterate over each character in the string
Add it to the StringBuilder if Character.isWhitespace() returns false
Avatar of Jay Roy

ASKER

well, how about a simple string.replace(" ","").trim()
Does using StringBuilder give me any performance benefit ?

thanks.
String.replace() will likely do more work than using the StringBuilder yourself, but you're right that it should give you the result you want.
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial