java-String类

1、String类的概述
  * A:String类的概述
  * 通过JDK提供的API,查看String类的说明
  * 可以看到这样的两句话。
    * a:字符串字面值"abc"也可以看成是一个字符串对象。
    * b:字符串是常量,一旦被赋值,就不能被改变。

2、String类的构造方法
  * A:常见构造方法
    * public String():空构造
    * public String(byte[] bytes):把字节数组转成字符串
    * public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
    * public String(char[] value):把字符数组转成字符串
    * public String(char[] value,int index,int count):把字符数组的一部分转成字符串
    * public String(String original):把字符串常量值转成字符串

3、String类的常见面试题
  * 1.判断定义为String类型的s1和s2是否相等
    * String s1 = "abc";
    * String s2 = "abc";
    * System.out.println(s1 == s2);    //true
    * System.out.println(s1.equals(s2));    //true

    解释:常量池中没有这个字符串对象,就创建一个,如果有直接用即可
  * 2.下面这句话在内存中创建了几个对象?
    * String s1 = new String("abc");

    解释:创建两个对象,一个在常量池中,一个在堆内存中
  * 3.判断定义为String类型的s1和s2是否相等
    * String s1 = new String("abc");
    * String s2 = "abc";
    * System.out.println(s1 == s2);    //false
    * System.out.println(s1.equals(s2));   //true

    解释:s1记录的是堆内存对象的地址值,s2记录的是常量池中的地址值。(用new新创建的字符串是该参数字符串的副本)
  * 4.判断定义为String类型的s1和s2是否相等
    * String s1 = "a" + "b" + "c";
    * String s2 = "abc";
    * System.out.println(s1 == s2);    //true
    * System.out.println(s1.equals(s2));   //true

    解释:java中有常量优化机制。例如:byte b = 3 + 4; //在编译时就变成7,把7赋值给b,常量优化机制。
  * 5.判断定义为String类型的s1和s2是否相等
    * String s1 = "ab";
    * String s2 = "abc";
    * String s3 = s1 + "c";
    * System.out.println(s3 == s2);   //false
    * System.out.println(s3.equals(s2));   //true

    解释:拼接的时候会在堆内存中调用StringBuffer对象,拼接后再调用toString转换成String对象,从而在堆内存中生成了新对象。

4、String类的判断功能
  * A:String类的判断功能
    * boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
    * boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
    * boolean contains(String str):判断大字符串中是否包含小字符串
    * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
    * boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
    * boolean isEmpty():判断字符串是否为空。

5、String类的获取功能
  * A:String类的获取功能
    * int length():获取字符串的长度。
    * char charAt(int index):获取指定索引位置的字符
    * int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
    * int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
    * int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
    * int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
    * int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。 

    * int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引。 

    * int lastIndexOf(int ch, int fromIndex):返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
    * int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。

    * String substring(int start):从指定位置开始截取字符串,默认到末尾。
    * String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。(左闭右开)

6、String类的转换功能
  * A:String的转换功能:
    * byte[] getBytes():把字符串转换为字节数组。
    * char[] toCharArray():把字符串转换为字符数组。
    * static String valueOf(char[] chs):把字符数组转成字符串。
    * static String valueOf(int i):把int类型的数据转成字符串。
    * 注意:String类的valueOf方法可以把任意类型的数据转成字符串

    * String toLowerCase():把字符串转成小写。
    * String toUpperCase():把字符串转成大写。
    * String concat(String str):把字符串拼接。

7、String类的其他功能
  * A:String的替换功能及案例演示
    * String replace(char old,char new)
    * String replace(String old,String new)
  * B:String的去除字符串两空格及案例演示
    * String trim()
  * C:String的按字典顺序比较两个字符串及案例演示
    * int compareTo(String str)
    * int compareToIgnoreCase(String str)

原文地址:https://www.cnblogs.com/hfumin/p/10177368.html