Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

Remove leading and Trailing spaces

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
micro_learner
Asked:
micro_learner
  • 5
  • 2
2 Solutions
 
zzynxSoftware engineerCommented:
trim()
0
 
cjjcliffordCommented:
String.trim()

This will remove leading/trailing whitespace...
0
 
zzynxSoftware engineerCommented:
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!

 
zzynxSoftware engineerCommented:
So:
>> does this remove new Line characters too ?
Yes
0
 
zzynxSoftware engineerCommented:
So

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

prints out

<Hello world>
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
 
cjjcliffordCommented:
:-)
0

Featured Post

Industry Leaders: 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!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now