Package com.day7.one;
public class DemoString2 {
/**
* @param args
* Get function of String class
* 1.int length (); Get string Lengths
* 2.char charAt (int index); Gets the character at the specified index position
* 3.int indexOf (int ch); Returns the index at the first occurrence of the specified character in this string
* 4.int indexOf (string str); Returns the index at the first occurrence of the specified string in this string
* 5.int indexOf (int ch,int fromIndex); Returns the index at the first occurrence of the specified character from the specified position in this string
* 6.int indexOf (string str,int fromIndex); Returns the index at the first occurrence of the specified string from the specified position in this string
* 7.lastIndexOf
* 8.String substring (int start); Intercept string from specified position, default to end
* 9.String substring (int start,int end); Intercept string from the specified position to the specified position
*/
public static void Main (string[] args) {
String s1= "Kobebryante";
System.out.println (S1.length ());
String s2= "Kobe Bryant";
System.out.println (S2.length ());//1. Get the length of a string
Char C1=s1.charat (0);
SYSTEM.OUT.PRINTLN (C1);//2. Output k
int Index=s1.indexof (' K ');
SYSTEM.OUT.PRINTLN (index);//3. Output 0
System.out.println (S1.indexof (' z '));//If not present, return-1
int Index1=s1.indexof ("be");
System.out.println (index1);//4. Gets the position of the first character in the "be" string
System.out.println (S1.indexof ("or"));//Return-1
int Index2=s1.indexof (' e ', 4);
System.out.println (INDEX2)//5. Look back from the specified position, output 10, and look back from the 4th one.
int Index3=s1.indexof ("NT", 4);
System.out.println (INDEX3);//6. Output 8
int Index4=s1.lastindexof (' e ');
System.out.println (index4);//7. Output 10, looking forward from the back, the first occurrence of the character
System.out.println (S1.indexof (' e '));//Output 3
String str1=s1.substring (4);
System.out.println (str1);//8. including the 4 position
String str2=s1.substring (4,6);
System.out.println (str2);//9. Output BR, including 4 not including 6 position; [4,6]
Trap questions
String s= "Kobe";
S.substring (2);
System.out.println (s);//print kobe,s unchanged, if System.out.println (s.substring (2));
System.out.println (s.substring (2));
}
}
Method of obtaining Java-string Class (IndexOf (); substring (), etc.)