?
Solved

Remove leading and Trailing spaces

Posted on 2005-03-21
7
Medium Priority
?
499 Views
Last Modified: 2012-05-05
I have been a Perl programer all through ..I was wondering if there is a good way ( in terms of Lines of code and not having to call extra API's)
to remove leading and trailing spaces.

Also ..does this remove new Line characters too ?
0
Comment
Question by:micro_learner
[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
  • 5
  • 2
7 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13591611
trim()
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 600 total points
ID: 13591617
String.trim()

This will remove leading/trailing whitespace...
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13591636
Returns a copy of the string, with leading and trailing whitespace omitted.

This method may be used to trim whitespace from the beginning and end of a string;
in fact, it trims all ASCII control characters as well.

whitespace:
'\t' '\u0009' HORIZONTAL TABULATION
'\n' '\u000A' NEW LINE
'\f' '\u000C' FORM FEED
'\r' '\u000D' CARRIAGE RETURN
' ' '\u0020' SPACE
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 37

Accepted Solution

by:
zzynx earned 1400 total points
ID: 13591645
So:
>> does this remove new Line characters too ?
Yes
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13591788
So

System.out.println("<" + "      Hello world          \n\n\n".trim() + ">");

prints out

<Hello world>
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13591796
Thanks for accepting
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 13591859
:-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

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