Package J2se;public class Stringdemo {private String demostring= "ceshixiaoyouning";p ublic void TestString () {String dsg= Demostring.concat ("very beautiful");//String addition can also be used to represent System.out.println (DSG); int len=demostring.length ();// The length of the string System.out.println (len); Boolean eq= "ceshixiaoyouning". Equals (demostring);//Compares two strings for equality System.out.println ( EQ); String Sub=demostring.substring (5, 8);//substring, starting with the 5th character, exactly 8 characters, but not containing the 8th character System.out.println (sub); String substring=demostring.substring (5);//substring, starting from the 5th character until the end of the string System.out.println (subString); Boolean SW = Demostring.startswith ("Ceshi");//Determine whether to start with a string System.out.println (SW); Boolean ew=demostring.endswith ("youning"); /Judge the stone at the end of a string System.out.println (EW); int Subindex=demostring.indexof ("ce");//Find out the first occurrence of a substring in a string, If not found, returns -1SYSTEM.OUT.PRINTLN (subindex), int lastindex=demostring.lastindexof ("i");//Find the substring at index of the last occurrence of the string, Returns -1SYSTEM.OUT.PRINTLN (LastIndex) If it is not found; System.out.println (Demostring.touppercase ());//The words in the string all become uppercase System.out.println (Demostring.tolowercase ());//The words in the string all become lowercase System.out.println ("youning". Trim ());//Remove String Subreplace=demostring.replace ("Ceshi", " Hello ");//Replace a string in a string with a new string System.out.println (Subreplace); String Subreplacef=demostring.replacefirst ("I", "HHH");//Converts the first occurrence of a substring into a new string, supporting a regular System.out.println ( SUBREPLACEF); String Subreplacea=demostring.replaceall ("I", "HHH");//Convert all substrings appearing in the string into new substrings, supporting regular System.out.println ( SUBREPLACEA);} public static void Main (string[] args) {Stringdemo s=new stringdemo (); s.teststring ();}}
Operation Result:
Ceshixiaoyouningvery Beautiful
16
True
Xia
Xiaoyouning
True
True
0
13
Ceshixiaoyouning
Ceshixiaoyouning
Youning
Helloxiaoyouning
Ceshhhhxiaoyouning
Ceshhhhxhhhaoyounhhhng
java-Test Development string