字符串学习笔记(一)---- String介绍

一、String类的特点

1、字符串对象一旦被初始化就不会被改变;

(1)常见问题

  • a
public static void main(String[] args) {
    String a = "abc";
    a = "nbc";
    System.out.println(a);
}

这种不叫作字符串对象改变,只是引用变量a的引用对象发生了改变;真正的“abc”字符串对象并没有改变;

  • b
public static void main(String[] args) {
    String a1 = "abc";
    String a2 = "abc";
    System.out.println(a1==a2);//true
}

结果为true,当然表示a1、a2引用字符串对象的地址是一样的。
原因是:在创字符串对象时,会有一个叫字符串缓冲池来存放创建的字符串对象。每创建字符串对象之前,会先到字符串缓冲池查看是否有这个字符串对象,如果有,直接将字符串缓冲池里的该对象的地址给它;如果没有,才会再进行创建新的字符串对象。

  • c
public static void main(String[] args) {
    String a1 = "abc";
    String a3 = new String("abc");
    System.out.println(a1==a3);//false
}

结果为false,表示a1与a3引用的变量地址不同。
原因是:所有new出来的对象是放在“堆”里面的,而a1引用的变量是放在常量池(字符串缓冲池)的,自然俩者的地址不同、引用的变量不同。

  • d
    public static void main(String[] args) {
        String a1 = "abc";
        String a3 = new String("abc");
        System.out.println(a1.equals(a3));//true
    }

String类对equals()方法进行复写,加上了比较俩个字符串内容的功能;这里为true,是因为俩个字符串对象的内容相同;

二、String类的构造函数

1、String s = new String()

(1)三种创建字符串对象的区别

public static void main(String[] args) {
        String a1 = new String();//在堆中,创建一个空字符串
        String a2 = "";//在字符串缓冲池中,创建一个空字符串
        String a3 = null;//引用型变量a3没有引用任何字符串对象
}

2、String s = new String(byte[] bytes);

(1)例子

public static void main(String[] args) {
    byte[] arr = {65,66,67,68};
    String s1 = new String(arr);//默认以ASCII码表解码
    System.out.println(s1);//ABCD
}

(2)指定解码来创建

String s = new String(byte[] bytes,Charset charset);

3、String s = new String(char[] value);

(1)例子

public static void main(String[] args) {
    char[] arr = {'a','b','c','d','e','f','g'};
    String s1 = new String(arr);//abcdefg
    System.out.println(s1);
}

(2)扩展:String s = new String(char[] value,int offset, int count);
参数介绍:

  • char[] value:要进行转换成字符串对象的字符数组;
  • int offset:从哪里开始截取的字符串数组的下标,从0开始;
  • int count:要截取字符串数组的长度;
public static void main(String[] args) {
    char[] arr = {'a','b','c','d','e','f','g'};
    String s2 = new String(arr, 0, 3);//第一个从0开始,取3个元素
    System.out.println(s2);//abc
}

三、String类的常见功能

1、获取

  • 获取字符串的长度
    int length();
  • 获取指定位置的字符
    char charAt(int index);
  • 获取指定字符在该字符串第一次出现的位置
    int indexOf(int ch);
    int indexOf(int ch,int fromIndex);
    int indexOf(String str);
    int indexOf(String str,int fromIndex);
    注意:返回值若为-1,表示该字符在该字符串中不存在;
  • 获取指定字符在该字符串最后一次出现的位置
    int lastIndexOf(int ch);
    int lastIndexOf(int ch,int fromIndex);//从后往前查找
    int lastIndexOf(String str);
    int lastIndexOf(String str,int fromIndex);
    注意:返回值若为-1,表示该字符在该字符串中不存在;
  • 获取字符串的一部分
    String substring(int beginIndex,int endIndex);//从beginIndex到endIndex-1结束;
    String substring(int beginIndex);
    注意:第一个的“从beginIndex到endIndex-1结束”是有原因的;“str.substring(beginIndex,str.length);”与“str.substring(beginIndex)”的功能相同,方便使用而已。

2、转换

  • 将字符串按照正则表达式分割成字符串数组
    String[] split(String regex);
  • 将字符串转化成字符数组
    char[] toCharArray();
  • 将字符串转化成字节数组
    byte[] getBytes();
  • 将字符串中的字母转换成大、小写字母
    String toUpperCase():大写
    String toLowerCase():小写
  • 将字符串内容进行替换
    String replace(char oldch,char newch);
    String replace(String oldstr,String newstr);
  • 将字符串俩端空格去除
    String trim();
  • 将俩个字符串进行连接
    String concat(String addStr);

3、判断

  • 判断俩个字符串内容是否相同
    boolean equals(Object obj);
    boolean equalsIgnoreCase(String str);//忽略大小写比较字符串内容
  • 判断字符串是否包含指定字符串
    boolean contains(String str);
  • 判断字符串是否以指定字符串开头或结尾
    boolean startsWith(String str);
    boolean endsWith(String str);
原文地址:https://www.cnblogs.com/fourther/p/7600688.html