String, Character
You can enter a random string to determine whether to start with a specified character and end with the specified character:
1 // enter a random string to determine whether to end with AB starting with AB. 2. SC = new signature (System. in); 3 System. out. println ("enter a String:"); 4 String str = SC. next (); 5 // class query 6 if (str. startsWith ("AB") {7 System. out. print ("string starting with AB,"); 8} else {9 System. out. print ("the string does not start with AB,"); 10} 11 if (str. endsWith ("AB") {12 System. out. println ("ends with AB. "); 13} else {14 System. out. println (" does not end with AB. "); 15} 16 // index query 17 int num1, num2; 18 num1 = str. indexOf ("AB"); 19 num2 = str. lastIndexOf ("AB"); 20 if (num1 = 0 & num2 = str. length ()-2) {21 System. out. println ("this string starts with AB and ends with AB. "); 22} else if (num1 = 0 & num2! = Str. length ()-2) {23 System. out. println ("this string only starts with AB,"); 24 25} else if (num1! = 0 & num2 = str. length ()-2) {26 System. out. println ("this string ends with AB. "); 27} else {28 System. out. println (" this string neither starts with AB nor ends with AB. "); 29} 30 31 // truncate String query 32 String str1 = str. substring (0, 2); 33 String str2 = str. substring (str. length ()-2, str. length (); 34 if (str1.equals ("AB") {35 System. out. print ("string starting with AB,"); 36} else {37 System. out. print ("the string does not start with AB,"); 38} 39 if (str2.equals ("AB") {40 System. out. println ("ends with AB. "); 41} else {42 System. out. println (" not ended with AB. "); 43}
Running result: