String s1 = "hello World!";
String s2 = new String( "Hello, World" );
String s1 = "Hello";
s1.length();
s1.charAt( 0 );//可将字符串理解为字符数组
"Hello World".length();
" hello ".trim();
String str = "Welcome to Java";
System.out.println( "The length of " + str + " is " + str.length() );
String str = "Welcome to Java";
int index = 0;
System.out.println( "The " + index + "th character of " + str + " is " + str.charAt( index ) );
String s1 = "welcome";
String s2 = "WELCOME";
System.out.println( "s1 to upper case: " + s1.toUpperCase() );
System.out.println( "s2 to lower case: " + s2.toLowerCase() );
String s3 = "\t Good Night \n";
System.out.println( s3.trim() );
String s4 = " H E L L O ";
String[] strs = s4.split( " " );
for( String str:strs )
System.out.print( str );
Scanner input = new Scanner( System.in );
String s1 = input.next();
String s2 = input.next();
String s3 = input.next();
System.out.println( "s1 + s2 + s3 is " + s1 + s2 + s3 );
System.out.print( "enter a line" );
String s = input.nextLine();
System.out.println( "s is " + s );
Scanner input = new Scanner( System.in );
String s = input.nextLine();
char ch = s.charAt( 0 );
System.out.println( "The character is " + ch );
String s1 = "Welcome";
String s2 = "Welcome";
String s3 = "welcome";
System.out.println( s1.equals( s2 ) );
System.out.println( s1.equals( s3 ) );
System.out.println( s1.equalsIgnoreCase( s3 ) );
System.out.println( s1.compareTo( s2 ) );
System.out.println( s1.compareTo( s3 ) );
System.out.println( s1.compareToIgnoreCase( s3 ) );
System.out.println( s1.startsWith( "We" ) );
System.out.println( s1.startsWith( "we" ) );
System.out.println( s2.endsWith( "me" ) );
System.out.println( s1.contains( "lco" ) );
System.out.println( s1.contains( "LCO") );
String str = "Welcome to Java";
String substr1 = str.substring( 4 );
String substr2 = str.substring( 0, 11 ) + "HTML";
System.out.println( substr1 );
System.out.println( substr2 );
System.out.println( str.indexOf( 'W' ) );
System.out.println( str.indexOf( 'o' ) );
System.out.println( str.indexOf( 'o', 5 ) );
System.out.println( str.indexOf( "come" ) );
System.out.println( str.indexOf( "Java", 5 ) );
System.out.println( str.indexOf( "java", 5 ) );
System.out.println( str.lastIndexOf( 'W' ) );
System.out.println( str.lastIndexOf( 'o' ) );
System.out.println( str.lastIndexOf( 'o', 5 ) );
System.out.println( str.lastIndexOf( "come" ) );
System.out.println( str.lastIndexOf( "Java", 5 ) );
System.out.println( str.lastIndexOf( "java", 5 ) );
针对八种基本数据类型,提供对应的包装类,用于实现类型间转换,如数字与字符串间的转换;也可作为参数传入相应方法,如各种泛型类方法,要求参数类型为Object类,基本数据类型无法传入
int n = 10;
Integer N = new Integer( n );
int m = N.intValue();
Integer N = 100; // 自动装箱,相当于new Integer(100)
int n = N; // 自动拆箱,相当于N.intValue();
String s = "1234";
int n1 = Integer.parseInt( s );
int n2 = Integer.parseInt( s, 10 );
int n3 = Integer.parseInt( s, 16 );
int n4 = Integer.parseInt( "0101", 2 );
Integer n1 = Integer.valueOf( s );
Integer n2 = Integer.valueOf( s, 16 );
Integer n3 = Integer.valueOf( "0101", 2 );
String s = Integer.toString( 1000 );
String s1 = "hello";
String s2 = "hello";
String s3 = new String( "hello" );
System.out.println( "s1==s2? " + (s1==s2) + ", s1==s3? " + (s1==s3) );
System.out.println( "s1.equals(s2)? " + s1.equals( s2 ) + ", s1.equals(s3)? " + s1.equals(s3) );
String s1 = "hello";
System.out.println( s1.hashCode() );
System.out.println(System.identityHashCode(s1));
s1 = "hello2";
System.out.println( s1.hashCode() );
System.out.println(System.identityHashCode(s1));
StringBuffer sb = new StringBuffer( "Hello" );
int i = 101;
sb.append( " " ).append( "World" ).append( i ); //可任意添加
String str = sb.toString();