Java-Foundation (ii) Java.lang

Source: Internet
Author: User

The 1.String class provides many ways to intercept characters from a string object

1.1 Char charAt (int where)

1.2 void getChars (int sourcestart, int sourceend, char target[], int targetstart)

Here Sourcestart specifies the subscript at which the substring begins, sourceend specifies the subscript of the next character at the end of the substring. Therefore, the substring contains characters from Sourcestart to sourceend–1. The array of the obtained characters is specified by target. The subscript of the target to which the substring is copied is specified by Targetstart. Note It is important to ensure that the array target should be large enough to accommodate the characters in the specified substring.

1.3 GetBytes ()

There is a method called GetBytes (), which is an alternative to implementing the GetChars () method that stores characters in a byte array, which uses the default character-to-byte conversion provided by the platform. Here is the simplest form of it: byte[] getBytes () can also use other forms of the GetBytes () method. GetBytes () is most useful when you are outputting a string value to an environment that does not support 16-bit Unicode encoding. For example, most Internet protocols and text file formats use 8-bit ASCII encoding when text is exchanged.

1.4 ToCharArray ()

The simplest way to convert a character in a string object to a character array is to call the ToCharArray () method. Corresponds to the entire string, which returns an array of characters. The general form is: char[] ToCharArray () This function is provided for ease of use, so the same result can be obtained with the GetChars () method.

1.5 equals () and Equalsignorecase ()

1.6 regionmatches () regionmatches ()

Method compares the interval specified in one string to the interval specified in another string. Its overloaded form allows the case to be ignored when comparing. The general form of the two methods is given below: Boolean regionmatches (int startIndex, String str2, int str2startindex, int numChars) Boolean Regionmatche S (boolean ignoreCase, int startIndex, String str2, int str2startindex, int numChars) for both of these forms, STARTINDEX Specifies the subscript that begins within the interval of the call string (String) object. The string used for the comparison is specified by STR2. Within the STR2, the subscript that begins the comparison interval is specified by the Str2startindex. The length of the substring used to compare is in NumChars. In the second scenario, if IgnoreCase is true, the case of the character is ignored. Otherwise, capitalization is meaningful.

1.7 StartsWith () and EndsWith ()

A string (string) defines two routines, which are more or less a special form of the Regionmatches () method. The StartsWith () method determines whether a given string (string) starts with a specified string. Conversely, the EndsWith () method determines whether the string (string) being discussed ends with a specified string. They have the following general form: Boolean StartsWith (String str) Boolean endsWith (String str)

Comparison of 1.8 equals () and = =

It is important to understand that the Equals () method and the = = operator are performing two different operations. As explained earlier, the Equals () method compares the characters in a string (String) object. the = = operator Compares two object references to see if they reference the same instance. The following program illustrates how two different string objects are able to package 248 Part 2 of the Java library with the same characters, but at the same time these object references are not equal

1.9

IndexOf () Searches for the first occurrence of a character or substring. •

LastIndexOf () Searches for the last occurrence of a character or substring.

2.0

Java-Foundation (ii) Java.lang

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.