First, get string information
Strings are used as objects, and the effective information of the string can be obtained through corresponding methods, such as obtaining the length of a string, the characters at a certain index position, and so on.
1.1 Get the length of the string
Use the length() method of the String class to obtain the length of a declared string object.
Syntax: str.length();
str : String object
String str = "We are Happy!"; int size = str.length(); //Assign the length of the string str to the int variable size
The value of size is equal to 13, which means that the length of the string returned by the length() method includes spaces in the string.
1.2 String lookup
The String class provides two methods for finding strings, the indexOf() and lastIndexOf() methods. Both of these methods allow searching within a string for a character or string of specified criteria. The indexOf() method returns the position of the first occurrence of the searched character or string, and the lastindexOf() method returns the position of the last occurrence of the searched character or string.
1.2.1 indexOf ( String s )
This method is used to return the index position of the first occurrence of the parameter string s in the specified string. When the indexOf() method of a string is called, the position of s is searched from the beginning of the current string; if the string s is not retrieved, the return value of this method is -1.
Syntax: str.indexOf(substr)
str : any string object
substr : the string to search for
1.2.2 lastindexOf( String str)
This method returns your index position of the last early summer of the specified string. When the string's lastindexOf() method is called, the parameter character str is retrieved from the beginning of the current string, and the index of the last occurrence of str is returned. If the string str is not retrieved, the method returns -1.
Syntax: str.lastindexOf( substr )
str : an arbitrary string object
substr : the string to search for
public class Text{ //Create class public static void main(String [] args){ //Main method String str = "We are students"; //Define the string str int size1 = str.indexOf("a"); //Assign the index position of a in the string str to the variable size1 int size2 = str.lastindexOf(""); //Assign the index position of the empty string in str to the variable size2 System.out.println("The index position of a character in the string str is: "+size1); //Output the variable size1 System.out.println("The index position of the empty string in the string str is: "+size2); //Output the variable size2 System.out.println("The length in the string str is: "+str.length()); //Output the length of str } }
The running result is:
The character's index position in the string str is: 3 The index position of the null character in the string str is: 15 The length of the string str is: 15
If the parameter in the lastindexOf() method is the empty string "" (note no spaces), the result returned is the same as the result of calling the length() method on that string.
1.3 Get the character at the specified index position
Use the charAt() method to return the character at the specified index.
Syntax: str.charAt(int index)
str : any string
index : an integer value that specifies the subscript of the character to be returned
public class Ref{ //Create class public static void main(String[] args){ //Main method String str = "hello word"; //Define the string str char mychar = str.charAt(5); //return the character at index position 5 in the string str System.out.println("The character at index position 5 in the string str is: "+mychar); //Output information } }
The result of the operation is: the character at index position 5 in the string str is: w
For learning Java, read books, watch videos, watch official documents, watch API, practice more, think more.
Always learn, update quickly, learn new technologies, work hard, and I wish you will become a great god in the end.