String a ="a b c ";String b ="A B C ";System.out.println("length:"+ a.length());System.out.println("charAt 0:"+ a.charAt(0));System.out.println("toCharArray:"+Arrays.toString(a.toCharArray()));System.out.println("equals:"+ a.equals(b));System.out.println("equalsIgnoreCase:"+ a.equalsIgnoreCase(b));System.out.println("compareTo:"+ a.compareTo(b));System.out.println("contains b:"+ a.contains("b"));System.out.println("contentEquals:"+ a.contentEquals("a b c"));System.out.println("startsWith a:"+ a.startsWith("a"));System.out.println("endsWith b:"+ a.endsWith("b"));System.out.println("indexOf b:"+ a.indexOf("a b"));System.out.println("lastIndexOf b:"+ a.lastIndexOf("b"));System.out.println("substring 0:"+ a.substring(0));System.out.println("substring 0~2:"+ a.substring(0,3));System.out.println("concat bbb:"+ a.concat("bbb"));System.out.println("replace b e:"+ a.replace("b","e"));System.out.println("toUpperCase:"+ a.toUpperCase());System.out.println("toLowerCase:"+ a.toLowerCase());System.out.println("trim:"+ a.trim());System.out.println("String.valueOf 100:"+String.valueOf(100));String s =String.format("你好:%s, 这是你来到这的第%d天,截止到目前,你的累计收入为:%.2f元","lala",30,7850.90);System.out.println(s);
输出:
length:6
charAt 0:a
toCharArray:[a,, b,, c,]
equals:false
equalsIgnoreCase:true
compareTo:32
contains b:true
contentEquals:false
startsWith a:true
endsWith b:false
indexOf b:0
lastIndexOf b:2
substring 0:a b c
substring 0~2:a b
concat bbb:a b c bbb
replace b e:a e c
toUpperCase:ABC
toLowerCase:a b c
trim:a b c
String.valueOf 100:100
你好:lala, 这是你来到这的第30天,截止到目前,你的累计收入为:7850.90元。
常用的类型转换字符
字符
类型
d
十进制整数
c
Unicode字符
b
Boolean值
s
String
f
十进制浮点数
e
科学计数法表示的浮点数
x
十六进制整数
h
十六进制散列码
%
字符“%”
Scanner
Scanner scanner =newScanner("wangwang\n10\nrun");String name = scanner.nextLine();System.out.println("name = "+ name);int age = scanner.nextInt();System.out.println("age = "+ age);String hobby = scanner.next();System.out.println("hobby = "+ hobby);
输出:
name = wangwang
age =10
hobby = run