1.Get Method
int length () Gets the length of the string,
char charAt (int index) gets the character at a specific position ( the corner label is out of bounds ),
int indexOf (String str) gets the position of a specific character (overload), finds the index value of the first occurrence of the substring, and returns 1 if it is not found.
int lastIndexOf (int ch) Gets the position of the last character, finds the last index value of the substring, and returns 1 if not found.
Public class Demo02 { publicstaticvoid main (string[] args) { String str= " ABC China AB China "; System.out.println ("Number of characters:" +str.length ()); System.out.println ("gets the corresponding character according to the index value:" +str.charat (1)); System.out.println ("Find the index value of the first occurrence of a substring:" +str.indexof ("AB")); System.out.println ("Find the last index value of the substring:" +str.lastindexof ("China"));} }
Operation Result:
Number of characters: 9
Gets the corresponding character according to the index value: b
Find the index value of the first occurrence of a substring: 0
Find the last index value of a substring: 7
2. Judgment method
Whether Boolean endsWith (String str) ends with the specified character
The Boolean isEmpty () is 0 Long if:
Whether Boolean contains (charsequences) contains the specified sequence of applications: Search
Boolean equals (Object anobject) is equal
Boolean equalsignorecase (String anotherstring) ignores case equality
Public classDemo03 { Public Static voidMain (string[] args) {String str= "Demo.java"; System.out.println ("Whether to start with the specified string:" +str.startswith ("De")); System.out.println ("Whether to end with the specified string:" +str.endswith ("Java")); System.out.println ("Determines whether the string content is empty:" +str.isempty ()); System.out.println ("Determines whether a string contains the specified content:" +str.contains ("Emo")); System.out.println ("Determine if the contents of 2 strings are consistent:" + "Demo.java". Equals (str)); System.out.println ("Determines whether the contents of 2 strings are consistent (ignoring case comparisons):" + "Demo.java". Equalsignorecase (str)); }}
Execution Result:
Whether to start with the specified string: True
Whether to end with the specified string: True
Determines whether the string contents are empty: false
Determines whether the string contains the specified content: true
Determine if the contents of 2 strings are consistent: false
Determines whether the contents of 2 strings are consistent (ignoring case comparisons): True
Java string Common methods < two >