String构造方法
package com. bjpowernode. javase. string ;
public class StringTest01 {
public static void main ( String [ ] args) {
String s1= "abc" ;
String s2= "abc" + "bef" ;
String s3= new String ( "abc" ) ;
}
}
package com. bjpowernode. javase. string ;
public class StringTest02 {
public static void main ( String [ ] args) {
String s1= "aa" ;
String s2= "aa" ;
System . out. println ( s1== s2) ;
String a1= new String ( "123" ) ;
String a2= new String ( "123" ) ;
System . out. println ( a1== a2) ;
System . out. println ( a1. equals ( a2) ) ;
int i= 100 ;
String s= "abc" ;
}
}
package com. bjpowernode. javase. string ;
public class StringTest03 {
public static void main ( String [ ] args) {
String s1= "abc" ;
System . out. println ( s1) ;
byte [ ] bytes= { 97 , 98 , 99 } ;
String s2= new String ( bytes) ;
System . out. println ( s2) ;
String s3= new String ( bytes, 1 , 2 ) ;
System . out. println ( s3) ;
char [ ] chars= { '我' , '是' , '中' , '国' , '人' } ;
String s4= new String ( chars, 1 , 3 ) ;
System . out. println ( s4) ;
}
}
String类中所包含的方法
package com. bjpowernode. javase. string ;
import java. nio. charset. StandardCharsets ;
public class StringTest04 {
public static void main ( String [ ] args) {
char c= "中国人" . charAt ( 1 ) ;
System . out. println ( c) ;
int ret1= "abc" . compareTo ( "abc" ) ;
System . out. println ( ret1) ;
int ret2= "abcd" . compareTo ( "abce" ) ;
System . out. println ( ret2) ;
int ret3= "acb" . compareTo ( "abc" ) ;
System . out. println ( ret3) ;
int a1= "abcdigf" . compareToIgnoreCase ( "abcfaofi" ) ;
System . out. println ( a1) ;
String s1= "abc" ;
String s2= "bds" ;
String s3= s1. concat ( s2) ;
System . out. println ( s3) ;
String s4= "abcdef" ;
String s5= "ab" ;
System . out. println ( s4. contains ( s5) ) ;
String s6= "abcd" ;
String s7= "jkajfl" ;
System . out. println ( s6. endsWith ( "cd" ) ) ;
System . out. println ( s7. endsWith ( "fl" ) ) ;
String a2= "abc" ;
byte [ ] arr= a2. getBytes ( ) ;
System . out. println ( arr[ 1 ] ) ;
String a3= "akdfaojfoajsfd" ;
int b= a3. indexOf ( "f" ) ;
System . out. println ( b) ;
String a4= "dcsad" ;
System . out. println ( a4. isEmpty ( ) ) ;
String a5= "skaasjfafjdo" ;
System . out. println ( a5. lastIndexOf ( "o" ) ) ;
String a6= "akafdjaofj" ;
String a7= a6. replace ( "aka" , "afkdjf" ) ;
System . out. println ( a7) ;
String a8= "1990-12-2=3" ;
String [ ] aa= a8. split ( "-" ) ;
for ( int i= 0 ; i< aa. length; i++ )
{
System . out. println ( aa[ i] ) ;
}
String [ ] bb= aa[ 2 ] . split ( "=" ) ;
for ( int i= 0 ; i< bb. length; i++ )
{
System . out. println ( bb[ i] ) ;
}
String c2= "abfksafj" ;
boolean b1= c2. startsWith ( "b" ) ;
System . out. println ( b1) ;
String b2= "kdsfjaofjao" ;
System . out. println ( b2. substring ( 3 , 5 ) ) ;
String b3= "我是中国人" ;
char [ ] cc= b3. toCharArray ( ) ;
for ( int i= 0 ; i< cc. length; i++ )
{
System . out. println ( cc[ i] ) ;
}
String b4= "AFAJOG" ;
System . out. println ( b4. toLowerCase ( ) ) ;
String b5= "fjaojgfioas" ;
System . out. println ( b5. toUpperCase ( ) ) ;
String b6= " nihao nihao " ;
System . out. println ( b6. trim ( ) ) ;
String b7= String . valueOf ( true ) ;
System . out. println ( b7) ;
}
}
StringBuffer的用法
package com. bjpowernode. javase. Stringbuffer ;
public class StringBuffer {
public static void main ( String [ ] args) {
String s= "" ;
for ( int i= 0 ; i< 100 ; i++ )
{
s= s+ i;
System . out. println ( s) ;
}
}
}
进行字符串的追加
package com. bjpowernode. javase. Stringbuffer ;
public class StringBufferTest02 {
public static void main ( String [ ] args) {
StringBuffer stringBuffer= new StringBuffer ( ) ;
char [ ] chars= { 'a' , 'b' , 'c' , 'd' } ;
stringBuffer= stringBuffer. append ( chars, 1 , 2 ) ;
char charss= stringBuffer. charAt ( 1 ) ;
System . out. println ( charss) ;
StringBuffer sb= new StringBuffer ( 100 ) ;
sb. append ( 'a' ) ;
StringBuffer aa= sb. append ( 'b' ) ;
System . out. println ( aa) ;
}
}
StringBuilder的用法
package com. bjpowernode. javase. Stringbuffer ;
public class StringBuilderTest01 {
public static void main ( String [ ] args) {
StringBuilder stringBuilder= new StringBuilder ( ) ;
stringBuilder. append ( 100 ) ;
stringBuilder. append ( 'n' ) ;
StringBuilder ss= stringBuilder. append ( "niaho" ) ;
System . out. println ( ss) ;
}
}
8种数据类型的包装类
package com. bjpowernode. javase. integer ;
public class IntegerTest02 {
public static void main ( String [ ] args) {
Integer integer= new Integer ( 122 ) ;
System . out. println ( integer. toString ( ) ) ;
Integer integer1= new Integer ( "123" ) ;
System . out. println ( integer1) ;
}
}
package com. bjpowernode. javase. integer ;
public class IntegerTest04 {
public static void main ( String [ ] args) {
Integer i= 100 ;
System . out. println ( i) ;
int a= i;
System . out. println ( a) ;
}
}