April 27, 2016 _java Learning notes Common API (i) String in _java

Source: Internet
Author: User

1. String is a separate class in Java, but a special class that is specifically used to represent strings. The way it was created was simple, just like creating a variable in C

String astring = "This is a String."; The variable type is string, and the variable is named astring, and the contents are "this is a string."

When learning the API, there is a special way to create it. Because string is a class, it is certain that its constructor method can be used to create the corresponding object.

String astring = new String ("This is a string."); This method is not practical, just to investigate the understanding of string, here is the creation of a string object, the content is "..." a copy of the string object.

Note that the Astring object here is the same as the string content in the previous row, but the address value is different from the usual string.

2. Here are some examples of common methods used in string classes.

2.1, gets the method, mainly has obtains the length, looks up the specified character position and obtains the substring.

public int Length () {}; Returns the length of the string, as it would be with the length value of the array.

public char charAt (int index) {}; Returns the character of the specified position in the string.

public int indexOf (int ch) {}/public int indexOf (String str) {}; Returns the position of the specified character (ASCII code) or string in a string

public int indexOf (int ch, int fromIndex) {}/public int indexOf (string str, int fromIndex) {} returns the specified character (ASCII code) or the position of the string in the string, but Finds from a specified position (the second parameter is the specified position)

public int lastIndexOf () {} is similar to the indexof system described above, except that the only difference is to start looking at the end of the string.

public string subString (int beginindex) {}/public string subString (int beginindex, int endIndex) {} Returns a substring starting at the specified position to the end of the string or from the specified position to the end of the specified position. Note that the end position is the character with the endindex-1 position, that is, the total substring length is endindex-beginindex.

2.2, conversion method, mainly converted to a string array (cut), converted to a single character array

Public string[] Split (String regex) {}; Returns an array of strings that are cut, which is determined by the parameters (involving regular expressions)

Public char[] ToCharArray () {}; Returns a character array with the contents of each character of the string.

Public byte[] GetBytes () {}; Returns a byte array with the contents of each byte of the string, note that English is made up of one byte, and Chinese is made up of two bytes.

Public String trim () {}; Returns a string that is the original string that removes the leading and trailing spaces. Often used in development.

public static String valueOf (Object obj) {}; Returns a string that contains the string representation of the parameter. This method has a lot of overloaded content, which basically covers the types common in Java. This means that most types can be converted to string representations, even in the form of an object (except that there is no egg).

April 27, 2016 _java Learning notes Common API (i) String in _java

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.