The java.lang.String class of common Java APIs (with analysis and examples) _04

The String class represents strings, and all string literals (such as "abc" ) in Java programs are implemented as instances of this class.

Strings are constants; their value cannot be changed after creation. Because String objects are immutable, they can be safely shared.

public final class String//Source code
    implements java.io.Serializable, Comparable<String>, CharSequence

From the definition of the Java.lang.String class, the String class implements the java.io.Serializable (serializable) interface, the Comparable<String> (sortable) interface, and the CharSequence (readable character sequence) interface.


-------------------------------------------------- -ASCII code-----------------------------------------

Familiarity with ASCII is useful when working with characters (mostly numbers and letters)



-------------------------------------------------- -----Common object methods------------------------------------------ ---------

Common Object Methods
char charAt(int index) 

Returns the char value at the specified index.

Example: "Hello".charAt( 0 ) = you

Note: If Chinese appears in the code, you need to set the file encoding to ANSI encoding

(For example, the settings in notepad++ are: menu bar --> Encoding --> Convert to ANSI encoding)

int codePointAt(int index) 

Returns the character (Unicode code point) at the specified index.

Example: "Hello".codePointAt( 0 ) = 20320

Example: "Programming".codePointAt( 0 ) = 31243

int compareTo(String anotherString) 

 Compare two strings lexicographically.

Example: "Hello".compareTo("Programming") = -10923

-10923 = 20320 - 31243

int compareToIgnoreCase(String str)

Compares two strings lexicographically, regardless of case.

例:"Baa".compareToIgnoreCase( "aaa") = 1

String concat(String str) 

Concatenates the specified string to the end of this string.

: “Baa” .concat (“aaa”) = “Baaaaa”;

boolean contains(CharSequence s) 

Returns true if and only if this string contains the specified sequence of char values.

例:“Baa”.contains( "aa" ) = true;

boolean endsWith(String suffix) 

Tests whether this string ends with the specified suffix.

例:"Baa".endsWith( "aa" ) = true;

 boolean startsWith(String prefix) 

 Tests whether this string starts with the specified prefix.

例:“Baa”.startsWith( "aa" ) = false;

int hashCode() 

Returns the hash code for this string.

Formula: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

int indexOf(String str) 

Returns the index of the first occurrence of the specified string in this string, or -1 if the character does not occur.

例:“Coder666”.indexOf("Coder") = 0;

int indexOf(String str, int fromIndex)

Returns the index of the first occurrence of the specified string after the specified index of this string, or -1 if the character does not occur.

例:“Coder666_Coder666”.indexOf("Coder", 3) = 9;

intindexOf(char ch)  Returns the index of the first occurrence of the specified string in this string, or -1 if the character does not occur.
intindexOf(char ch, int fromIndex) Returns the index of the first occurrence of the specified string after the specified index of this string, or -1 if the character does not occur
int lastIndexOf(int ch) / (String str) Returns the index of the last occurrence of the specified character (string) in this string.
int lastIndexOf(int ch, int fromIndex)  Returns the index of the last occurrence of the specified character before the specified index in this string.
int length () 

Returns the length of this string.

例:“Coder666”.length() = 8

String    replace(char oldChar, char newChar) 

Returns a new string obtained by replacing all occurrences of oldChar in this string with newChar.

例:“Coder888”.replace( '8' , '6') = "Coder666";

String replace(CharSequence target, CharSequence replacement) 

返回一个新的字符串,它是通过用 新字符子串replacement 替换此字符串中出现的所有 就字符子串 target 得到的。

例:"Coder888_Coder888".replace("888","666")

String[] split(String regex) 

根据给定正则表达式的匹配拆分此字符串。

例:"Coder888_Coder888".split( "_" ) = { "Coder888" , "Coder666" };

 String substring(int beginIndex, int endIndex) 

返回一个新字符串,它是此字符串的一个子字符串。

例:" Coder666_Java".substring( 5 , 8) = "666"

 char[] toCharArray() 

将此字符串转换为一个新的字符数组。

例:“Coder666”.toCharArray( ) = { ' C ' , ' o ' , ' d ' , ' e ' , ' r ' , ' 6 ' , ' 6 ' , ' 6 '}

String toLowerCase() 

将此 String 中的所有字符都转换为小写。

例:“Coder666”.toLowerCase() = "coder666"

 String toUpperCase() 

使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

例:“Coder666”.toUpperCase() = "CODER666"

String trim() 

返回字符串的副本,忽略前导空白和尾部空白。

例:“    Coder666    ”.trim() = "CODER666"

 boolean matches(String regex)  告知此字符串是否匹配给定的正则表达式。
   


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325785223&siteId=291194637