Use the jsp substring () function to extract strings.

Source: Internet
Author: User

Str = str. substring (int beginIndex); truncates the string whose length is beginIndex from the first letter, and assigns the remaining string to str;

Str = str. substring (int beginIndex, int endIndex); truncate the string from beginIndex to endIndex in str and assign it to str;

Instance

 

Public class Utils {
Public static String diff (String str1, String str2 ){
Int index = str1.lastIndexOf (str2 );
If (index>-1 ){
Return str1.substring (str2.length ());
    }
Return str1;
  }
}

Instance 2

Public class TestSubstring {

Public static void main (String [] args ){
String str1 = "fghjkl ";
String str2 = str1.substring (1); // truncate the String starting from position 1st, cut it to the end, and assign the return value to str2.
System. out. println ("str1 =" + str1 );
System. out. println ("str2 =" + str2 );
 }
}

Instance 3

Public static String substring (String str, int start, int end ){
If (str = null ){
Return null;
      }

// Handle negatives
If (end <0 ){
End = str. length () + end; // remember end is negative
      }
If (start <0 ){
Start = str. length () + start; // remember start is negative
      }

// Check length next
If (end> str. length ()){
End = str. length ();
      }

// If start is greater than end, return ""
If (start> end ){
Return "";
      }

If (start <0 ){
Start = 0;
      }
If (end <0 ){
End = 0;
      }

Return str. substring (start, end );
  }

}

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.