Java uses the substring function to intercept a string, substringstring

Source: Internet
Author: User

Java uses the substring function to intercept a string, substringstring
There are two substring () functions in String, as follows:

I. String. substring (int start)

Parameters:

Start: Index of the location to be intercepted

Return Value:

String from start to end

Example: String str = "hello word! ";
System. out. println (str. substring (1 ));

System. out. println (str. substring (3 ));

System. out. println (str. substring (6 ));

The result is as follows:

Ello word!

Lo word!

Ord!

If start is longer than the string length, an out-of-bounds exception is thrown;

Ii. String. substring (int beginIndex, int endIndex)

Parameters:

BeginIndex start Position Index

EndIndex end position index

Return Value:

String from beginIndex to endIndex

Example: String str = "hello word! ";

System. out. println (str. substring (1, 4 ));

System. out. println (str. substring (3,5 ));

System. out. println (str. substring (0, 4 ));

The result is as follows:

Ell

Lo

Hell

If either startIndex or endIndex is out of bounds, an out-of-bounds exception is thrown.

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.