字符串相關類: String, StringBuffer
String類為不可變的字符序列
String s1="hello"; String s2="hello"; System.out.println(s1==s2); //true s1=new String("hello"); s2=new String("hello"); System.out.println(s1==s2); //false System.out.println(s1.equals(s2)); //true char c[]={'s','u','n',' ', 'j','a','v','a'}; String s4=new String(c); String s5=new String(c,4,4); System.out.println(s4); // sun java System.out.println(s5); //java
String類常用方法:
public class Test{ public static void main(String[] args){ String s1="sun java", s2="sun Java"; System.out.println(s1.charAt(1)); //s System.out.println(s1.length()); //8 System.out.println(s1.indexOf("java")); //4 System.out.println(s1.indexOf("Java")); //-1 System.out.println(s1.equals(s2)); //false System.out.println(s1.equalsIgnoreCase(s2)); //true String s="我是程序員,我在學java"; String sr=s.replace("我","你"); System.out.println(sr); //你是程序員, 你在學java } }
public class Test{ public static void main(String[] args){ String s="Welcome to Java World!"; String s1=" sun java "; System.out.println(s.startsWith("Welcome")); //true System.out.println(s.endsWith("World")); //false String sL=s.toLowerCase(); String sU=s.toUpperCase(); System.out.println(sL); // Welcome to Java World! System.out.println(sU); // WELCOME TO JAVA WORLD! String subS=s.substring(11); System.out.println(subS); // Java World! String sp=s1.trim(); System.out.println(sp); //sun java } }
基本數據類型包裝類:
Math類
File類
枚舉類