String类
String类是JAVA提供的专门用来处理字符序列的类,String类在java.lang包内,java.lang包中的类是被默认引入的,所以在程序中可以直接使用String类。
注:JAVA把String类定义为final类,所以用户不能扩展String类,也就是说String类不能有子类。
String类中引入了常量池,String常量是对象,它是由双引号括起来的字符序列,例如"你好","再见","you"。
常量池如下:
注:常量池中的数据在程序运行期间不允许改变
1.String类声明和创造对象
String s1=new String("hello");
String s2=new String("hello");
虽然在这里s1和s2都是hello但是二者的引用不同,所以s1==s2的值是false(new运算符每次使用都会创造出新的)
无法输出String对象的引用,输出的是他的实体,hello。
System.out.println(s1);
2.String常用构造方法
(1)用String(char a[ ])用一个数组创建String对象
char a[]={'j','a','v','a'};
String s=new String(a);
相当于
String s=new String("java");
(2)String(char a[ ],int startIndex,int count)//startIndex和count代表的是在a里面开始和结束的位置
char a[]={'0','1','2','3','4','5','6','7','8'};
String s=new String(a,2,4);
相当于
String s=new String("2345");
注:在数组中第一个位置是0.
(3)引用String常量
String s1,s2;
s1="你好";
s2="你好";
s1和s2具有相同的引用,所以s1==s2的值是true;
(4)字符串的并置
String对象可以用+进行并置运算
public class Example8_1 {
public static void main(String args[]) {
String hello = "你好";
String testOne = "你"+"好"; //【代码1】
System.out.println(hello == testOne); //输出结果是true
System.out.println("你好" == testOne); //输出结果是true
System.out.println("你好" == hello); //输出结果是true
String you = "你";
String hi = "好";
String testTwo = you+hi; //【代码2】
System.out.println(hello == testTwo); //输出结果是false
String testThree = you+hi;
System.out.println(testTwo == testThree); //输出结果是false
}
}
String testTwo = you+hi,相当于 String testTwo = new String("你好");创建了新的引用。
这里面==判断的是对象的引用,判断实体可以用equals().
String s1=new String("hello");
String s2=new String("hello");
System.out.println(s1.equals(s2)); //true
System.out.println(s1 == s2); //false
(5)length();获取String对象的长度;
String s="123456";
int s1;
s1 = s.length();//s1=6
(6)判断字符序列前缀和后缀
String tom1 = "天气预报";
String tom2 = "火山爆发";
tom1.startsWith("天气");//true
tom2.startsWith("天气");//false
tom1.endsWith("爆发");//false
tom2.endsWith("爆发");//true
(7)compareTo(String s),按字典比较大小
String str="abcde";
str.compareTo("boy);//小于0
str.compareTo("aba);//大于0
str.compareTo("abcde);//等于0
(8)contains(String s)判断对象的字符序列是否包含s的字符序列
String tom = "later";
tom.contains(la);//true
tom.contains(bb);//false
(9)indexOf(String s)从字符序列位置0开始返回第一次遇到的s的位置,如果没遇到返回-1,lastIndexOf(String s)从右边最后出现的s到最左边出现的s且从开始为位置为0.
String tom = "I am an orange";
tom.indexOf("a");//值是2
(10)substring(int startpoint)这个可以理解为复制整个字符串然后将其中的一段摘出来
String tom = "我讨厌这个是世界";
String s=tom.substring(1,3);//s=讨厌
这个摘取的长度是从1到3-1,也就是1到2.
(11)字符串与基本数据之间的转换
将字符串转为int型,使用java.lang包中的Integer类的parseInt(String s)
int x;
String s ="123";
x = Integer.parseInt(s);
类似的要调用相应类方法
public static byte parseByte(String s) throws NumberFormatException
public static short parseShort(String s) throws NumberFormatException
public static long parseLong(String s) throws NumberFormatException
public static float parseFloat(String s) throws NumberFormatException
public static double parseDouble(String s) throws NumberFormatException
将数值转换为字符串valueOf(),先调用类方法
public static String valueOf(byte n)
public static String valueOf(int n)
public static String valueOf(long n)
public static String valueOf(float n)
例如将123.123转换为字符串
String str = String.valueOf(123.123);