一、String类
String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。二、String类对象的创建字符串声明:String stringName;字符串创建:stringName = new String(字符串常量);或stringName = 字符串常量;三、String类构造方法1、public String()无参构造方法,用来创建空字符串的String对象。 1 String str1 = new String(); 2、public String(String value)用已知的字符串value创建一个String对象。 1 String str2 = new String("asdf"); 2 String str3 = new String(str2); 3、public String(char[] value)用字符数组value创建一个String对象。1 char[] value = {'a','b','c','d'};2 String str4 = new String(value);//相当于String str4 = new String("abcd");
1 char[] value = {'a','b','c','d'};2 String str5 = new String(value, 1, 2);//相当于String str5 = new String("bc");
1 byte[] strb = new byte[]{ 65,66};2 String str6 = new String(strb);//相当于String str6 = new String("AB");
四、String类常用方法
1、求字符串长度public int length()//返回该字符串的长度1 String str = new String("asdfzxc");2 int strlength = str.length();//strlength = 7
1 String str = new String("asdfzxc");2 char ch = str.charAt(4);//ch = z
1 String str1 = new String("asdfzxc");2 String str2 = str1.substring(2);//str2 = "dfzxc"3 String str3 = str1.substring(2,5);//str3 = "dfz"
1 String str1 = new String("abc");2 String str2 = new String("ABC");3 int a = str1.compareTo(str2);//a>04 int b = str1.compareToIgnoreCase(str2);//b=05 boolean c = str1.equals(str2);//c=false6 boolean d = str1.equalsIgnoreCase(str2);//d=true
1 String str = "aa".concat("bb").concat("cc");2 相当于String str = "aa"+"bb"+"cc";
1 String str = "I am a good student";2 int a = str.indexOf('a');//a = 23 int b = str.indexOf("good");//b = 74 int c = str.indexOf("w",2);//c = -15 int d = str.lastIndexOf("a");//d = 56 int e = str.lastIndexOf("a",3);//e = 2
1 String str = new String("asDF");2 String str1 = str.toLowerCase();//str1 = "asdf"3 String str2 = str.toUpperCase();//str2 = "ASDF"
1 String str = "asdzxcasd";2 String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"3 String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"4 String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"5 String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"
1 String str = " a sd ";2 String str1 = str.trim();3 int a = str.length();//a = 64 int b = str1.length();//b = 4
1 String str = "asdfgh";2 boolean a = str.statWith("as");//a = true3 boolean b = str.endWith("gh");//b = true
1 String str = "student";2 str.contains("stu");//true3 str.contains("ok");//false
1 String str = "asd!qwe|zxc#";2 String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";
五、字符串与基本类型的转换
1、字符串转换为基本类型java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:1)public static byte parseByte(String s)2)public static short parseShort(String s)3)public static short parseInt(String s)4)public static long parseLong(String s)5)public static float parseFloat(String s)6)public static double parseDouble(String s)例如:1 int n = Integer.parseInt("12");2 float f = Float.parseFloat("12.34");3 double d = Double.parseDouble("1.124");
1 String s1 = String.valueOf(12);2 String s1 = String.valueOf(12.34);