SUBSTRING (), charAt (), IndexOf () in Java (2013-05-05-BD write log migration

Source: Internet
Author: User

Substring

1. public string substring (int beginindex) returns a new string that is a substring of this string,

The substring starts at the specified index and continues to the end of the string.

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

Return:
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 that is a substring of this string,

The substring starts at the specified beginindex, and length: Indicates the length of the substring.

Parameters:

Beginindex-the index (including) at the beginning.
Endindex at the end of the index (not included).

Return:
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 returns a string: "ell"; Str.substring (+)//return E

Str.substring (1)//Return to "Ello World";

There is also a strange phenomenon in this function, when str.substring (5,0) occurs; What the hell is going on here?

However, the return is "Hello", str.substring (5,1)//Return "Ello", truncate the first bit, return the remaining.

Visible substring (start,end), can have different instructions, that is, start can be the length to return, end is the number of characters to be removed

(starting from the first place).

char charAt (int n) gets a single character at the position specified by the parameter n. The first position of a sequence of strings in the current object entity is 0,

The second position is 1, and so on. The value of n must be a non-negative number.

The string calls the CharAt () method to extract one of the characters in the string, for example: string st= "123456" St.charat (3) =4

For example, "ABCDE" calls Chatat (0) The 0 is the index of a character in the string returns a call Chatat (1) returns B.

System.out.println ("AZ123Z1234Z12345Z123456Z1234567Z12345678Z449FK". IndexOf (' z ', 2));

Str.indexof (S1,index)

The use of this function is to find the characters in the string S1 to find from index, and return the index of the character that is in this meaning?

But do not understand why indexOf (' Z ', 2) and indexOf (' Z ', 3) return 5, First, indexOf (' Z ', 2) 2 represents the

string 3rd [Note that it is 3, it has skipped the first z] character to start looking for the ' z ' character, find the position where the ' Z ' character is returned, please note that

The number of positions returned at this time [you are 5] is also counted from the 0 position. So indexof (' Z ', 3) found the Z also and

IndexOf (' Z ', 2) The position of z found is the same.

For example:
String s = "Java is a" + "platform independent language";
System.out.println ("Index of t=" +s.indexof (' t ')); 12
System.out.println ("Last Index of t=" +s.lastindexof (' t ')); 27
System.out.println ("Index of (t,10) =" +s.indexof (' t ', 10)); 12
System.out.println ("Last Index of (t,60=)" +s.lastindexof (' t ', 60)); 27

SUBSTRING (), charAt (), IndexOf () in Java (2013-05-05-BD write log migration

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.