1 Packagedemo03;2 3 //how to construct a string class4 Public classStringdemo {5 Public Static voidMain (string[] args) {6 7String str1 = "str1";//definition Mode 18String str2 =NewString ("str1");//Definition Mode 29 Ten System.out.println (str1); One System.out.println (str2); A - //false STR1==STR2 compares the memory address -System.out.println (str1==str2); the //True the String class overrides Equals () to compare each character - System.out.println (Str1.equals (str2)); - - byte[] by = {97,98,99,100}; +String STR3 =NewString (by);//constructor Method String (byte[] bytes) byte array -System.out.println (STR3);//ABCD + AString STR4 =NewString (by,1,3);//constructor Method String (byte[] bytes int offset, int length) atSystem.out.println (STR4);//BCD - - Char[] Chararray = {' I ', ' j ', ' K '}; -String STR5 =NewString (Chararray);//constructor Method String (char[] value) character array -System.out.println (STR5);//Ijk - inString STR6 =NewString (chararray,1,2);//constructor Method String (char[] value, int offset, int count) -System.out.println (STR6);//JK to + - the * } $}
1 Packagedemo03;2 3 Public classStringDemo1 {4 Public Static voidMain (string[] args) {5String S1 = "ABCDEFGABCDEFG";6 7 intLength = S1.length ();//Get string length8System.out.println (length);// -9 TenString s2 = s1.substring (0,3);//intercept a string two parameters OneString s3 = s1.substring (7);//a parameter ASYSTEM.OUT.PRINTLN (S2);//ABC -System.out.println (S3);//ABCDEFG - the BooleanB1 = S1.startswith ("abc");//whether to start with the specified string -System.out.println (B1);//true - - BooleanB2 = S1.endswith ("EFG");//whether to end with the specified string +System.out.println (B2);//true - + BooleanB3 = S1.contains ("gab");//whether to include the specified string ASYSTEM.OUT.PRINTLN (B3);//true at - intindex1 = S1.indexof (' C ');//returns the index of the first occurrence of the specified character - intIndex2 = S1.indexof (' x '); -System.out.println (INDEX1);//2 -System.out.println (INDEX2);//-1 - in byte[] bytes = S1.getbytes ();//string to byte array - for(inti = 0; i < bytes.length; i++) { to System.out.println (Bytes[i]); + } - the Char[] ch = s1.tochararray ();//string to character array * for(inti = 0; i < ch.length; i++) { $ System.out.println (Ch[i]);Panax Notoginseng } - theString S5 = "ABC"; +String s6 = "ABC"; A BooleanB5 = s5.equals (S6);//Comparing Strings the BooleanB6 = S5.equalsignorecase (S6);//comparing strings ignoring case +System.out.println (B5);//false -System.out.println (B6);//true $ $ - - } the}
Demo
1 Packagedemo03;2 3 Public classStringlianxi {4 Public Static voidMain (string[] args) {5GetCount ("fhdkfadfdf123f");6System.out.println (Toconvert ("AVDFDFD"));7System.out.println (Getstringcount ("HELLOABC,ABC,ABCABCFJDKLSFJD", "ABC"));8 }9 Ten //gets the number of uppercase, lowercase, and numeric characters in the specified string One Public Static voidGetCount (String s) { A intUpper = 0; - intLower = 0; - intDigit = 0; the - for(inti = 0; I < s.length (); i++) { - Charc =S.charat (i); - + /*if (c>=65 && c<=90) { - upper++; + } else if (c>=97 && c<=122) { A lower++; at } else if (c>=48 && c<=57) { - digit++; - }*/ - - if(c>= ' A ' && c<= ' Z ') { -upper++; in}Else if(c>= ' A ' && c<= ' z ') { -lower++; to}Else if(c>= ' 0 ' && c<= ' 9 ') { +digit++; - } the } * $ System.out.println (upper);Panax Notoginseng System.out.println (lower); - System.out.println (digit); the } + A //capitalizes the first letter of the specified string, with other letters lowercase the Public Staticstring Toconvert (string s) { +String first = s.substring (0,1); -String after = s.substring (1); $First =first.touppercase (); $after =after.tolowercase (); - - returnfirst+After ; the } - Wuyi //Query the number of occurrences of other strings in the specified string the Public Static intGetstringcount (string s, string key) { - intCount = 0; Wu intindex = 0; - About while(index = S.indexof (key))! =-1) { $count++; -s = s.substring (index+key.length ()); - } - A returncount; + } the -}
Java String Class