The Trim method in Java's String class is implemented

Source: Internet
Author: User

Package Stringtest;public class StringDemo4 {public static void main (string[] args) {String str = "   abcdefg   "; System.out.println ("original string:"); System.out.println ("str = (" + str + ")"); StringDemo4 sd4 = new StringDemo4 (); System.out.println ("String after removing space:"); Sd4.sop (Sd4.mytrim (str)); Str.trim ();} String Mytrim (String str) {int start = 0;int end = Str.length () -1;/*** Note: Start <= end and Str.charat (start) = = ' Two conditions are not in order to Exchange, * otherwise, when the entire character representable is a space, the start value will be equal to str.length (), greater than the maximum angle Mark Str.length () -1* condition Str.charat (start) = = " Causes a corner mark to be out of bounds exception stringindexoutofboundsexception,*&& is the execution of the first condition is false, the second condition will not execute */while (start <= End & & Str.charat (start) = = ") {start++;} While (end >= start && str.charat (end) = = ") {end--;} Return str.substring (Start, end+1); }//Print string void sop (String str) {System.out.println ("str = (" + str + ")");}

Trim method implementation in the string class of Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.