Substring usage in java and javasubstring usage

Source: Internet
Author: User

Substring usage in java and javasubstring usage

Substring


1. public String substring (int beginIndex ).
Returns a new string, which is a substring of this string. The substring starts with a character at the specified index until the end of the string.


Parameters:
BeginIndex-the index at the beginning (including ).


Return Value:
The specified substring.


For example:
"Unhappy". substring (2) returns "happy"
"Harbison". substring (3) returns "bison"
"Emptiness". substring (9) returns "" (an empty string)

2. public String substring (int beginIndex, int length ).
Returns a new string, which is a substring of this string. The substring starts from the specified beginIndex. length: indicates the length of the substring.


Parameters:
BeginIndex-the index at the beginning (including ).
The index at the end of endindex (not included ).


Return Value:
The specified substring.


Example:
"Hamburger". substring (4, 8) returns "urge"
"Smiles". substring (1, 5) returns "mile"

3.

<Script type = "text/javascript">

Var str = "Hello world! "Document. write (str. substring (1, 3 ));

</Script>

 

The above return string: "ell"; str. substring () // returns e

 

Str. substring (1) // return "ello world ";

 

In addition, this function may have a strange phenomenon when str occurs. substring (5, 0); but the returned result is "hello", str. substring (5, 1) // return to "ello", cut the first digit, and return the remaining.

 

It can be seen that substring (start, end) can have different instructions, that is, start can be the length to be returned, and end is the number of characters to be removed (starting from the first place ).

 

In JS, substr (start, length) is easy to use.

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.