String常用方法及不可变性(Java)

认识String

String类在java.lang包中,Java中使用String类来创建字符串变量,该变量是一个对象(引用),具有不可变性;

String 类提供的构造方式非常多,常用的构造方式有以下三种;

String 类的构造方式

三种构造方式:

(1)使用常量字符串构造

         String s1="hello";

(2)采用 new String()对象构造

    String s2=new String("hello"); //有参数
     String s3=new String(); //无参数

(3) 使用字符数组构造

        char[] arr={'a','b'};
        String s4=new String (arr);

《理解String类的不可变性》

String类里面有哪些成员变量呢?

Ctrl+左键进入到该类的内部,就可以看到如下图1所示:

图1:
在这里插入图片描述
从上面可以看出:

(1) 里面包含valuehash两个成员变量;
(2) 该类被final修饰,表明该类不能被继承;
(3) 成员变量value也被final修饰;

那可能会认为其内部保存字符的数组被final修饰了因此它不可变,但实际上真是这样吗?
在这里插入图片描述

在这里插入图片描述
实质:库函数规定好的哦(没看错,就是这样),

图2:
在这里插入图片描述
接着来就看一下是不是因为其内部保存字符的数组valuefinal 修饰了,才导致它的内容不能被修改呢?

代码

public class TestString {
    public static void main(String[] args) {
       final char[] arr={'a','b','c','d'};
       String s1=new String (arr);
       arr[0]='A'; //0号位置修改为大写字母A
       System.out.println(arr[0]);
    }
}

结果输出:
在这里插入图片描述
结果表明:与是否被 final 修饰没有关系,valuefinal 修饰,表明value自身的值不能改变,即不能引用其它字符数组,但是其引用空间中
的内容可以被修改。

String类的常用方法

1. 获取字符串长度----length

方法:

  public int length()

代码:

public class TestString {
    public static void main(String[] args) {
    String str=new String ("1234abcd");
        int len= str.length();
        System.out.println(len);
    }
}

输出: 8

2. String对象的比较(四种方式

(1) 采用 ( ==) 比较两个引用变量引用的是否为同一个对象

代码:

public class TestString {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("world");
        String s4 = s1;
        System.out.println(s1 == s2); // false
        System.out.println(s2 == s3); // false
        System.out.println(s1 == s4); //true

(2) 采用 equals比较字符串里面的内容

方法:

   public  boolean equals(Object obj)

代码:

public class TestString {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("world");
        String s4 = s1;
        System.out.println(s1.equals(s2)); // true
        System.out.println(s1.equals(s3));//false
        System.out.println(s1.equals(s4));//true

(3)采用 compareTo 比较

方法:

     public  int  compareTo(String str)

代码:

public class TestString {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("abc");
        String s4 = new String("abcdef");
        System.out.println(s1.compareTo(s2));
        System.out.println(s1.compareTo(s3));
        System.out.println(s1.compareTo(s4));

equals 不同的是,equals返回的是 boolean 类型,而 compareTo 返回的是 int 类型。

具体比较方式:

1)先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值;
2) 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值;

(4) 采用 compareToIgnoreCase 比较----不区分大小写

方法:

  public int compareToIgnoreCase(String str)

代码:

 public class TestString {
    public static void main(String[] args) {
        String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("abc");
        String s4 = new String("abcdef");
        System.out.println(s1.compareToIgnoreCase(s2));         System.out.println(s1.compareToIgnoreCase(s3));        System.out.println(s1.compareToIgnoreCase(s4));

compareTo方式相同,只是忽略大小写比较;

3. 字符串查找-----(七种方式

  • (1) public char charAt(int index) :返回 index 位置上的字符;

代码:

public class DemoString {
    public static void main(String[] args) {
        String str=new String("aaaabbbbcccdddd");
        char ch=str.charAt(3);
        System.out.println(ch);  // 输出为 a

    }
}

当没有找到要输出的字符时,JVM 抛出IndexOutOfBoundsException 异常,如下所示:
在这里插入图片描述

  • (2)public int indexOf(int ch):返回 ch 第一次出现的位置;

代码:

public class DemoString {
    public static void main(String[] args) {
        String str=new String("aaaabbbbcccdddd");
        int i=str.indexOf('b');
        System.out.println(i);  // 输出为 4
        //上面的写法突出它的返回值类型
         //还可以写成以下方式
    System.out.println(str.indexOf('b'));// 4
    System.out.println(str.indexOf('e'));// 没有返回-1
 }

与上面不同的是:没有就返回 -1

  • (3)public int indexOf(int ch, int fromIndex):从fromIndex 位置开始找 ch 第一次出现的位置;

代码:

public class DemoString {
    public static void main(String[] args) {
    String str=new String("aaaabbbbcccdddd");
    System.out.println(str.indexOf('c',11)); // 没有返回-1
 System.out.println(str.indexOf('c',1)); //8
    }
}
  • (4)public int indexOf(String str):返回 str 第一次出现的位置,没有返回-1;

代码:

public class DemoString {
    public static void main(String[] args) {
   System.out.println(str.indexOf("cc",1)); // 8
  System.out.println(str.indexOf("ff"));  //没有返回-1
   }
}
  • (5)public int indexOf(String str, int fromIndex)
    fromIndex 位置开始找 str 第一次出现的位置,没有返回-1;

代码:

public class DemoString {
   public static void main(String[] args) {
 System.out.println(str.indexOf("cc",9));//9
 System.out.println(str.indexOf("cc",15));//没有返回-1
    }
}
  • (6)public int lastIndexOf(int ch):从后往前找 ch 第一次出现的位置;

代码:

public class DemoString {
   public static void main(String[] args) {
  System.out.println(str.lastIndexOf('d')); //14
 System.out.println(str.lastIndexOf('f'));//没有返回-1
    }
}
  • (7) public int lastIndexOf(String str):从后往前找 str 第一次出现的位置,没有返回-1;

代码:

public class DemoString {
   public static void main(String[] args) {
      System.out.println(str.indexOf("ccc"));//8
      System.out.println(str.indexOf("ee"));//-1

    }
}

4. 字符串的相互转化

1)大小写的相互转化

方法: public String toLowerCase() // 返回转化后小写的新字符串
public String toUpperCase() //返回转换后的大写的新的字符串

代码:

public class Demo {
    public static void main(String[] args) {
        String str=new String("aaBBc");
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
    }
}
输出结果:
aabbc
AABBC

2)基本类型转为字符串

方法:String.valueOf()

代码:

public class Demo {
    public static void main(String[] args) {
        String s1 = String.valueOf(12);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        System.out.println(s1);//12
        System.out.println(s2);//12.34
        System.out.println(s3);//true
    }
}

3 )字符串转数字借助包装类

public class Demo {
    public static void main(String[] args) {
        int data1=Integer.parseInt("123");
        double data2=Double.parseDouble("1.3");
        System.out.println(data1);//123
        System.out.println(data2);//1.3
    }
}

基本类型对应的包装类
在这里插入图片描述

4)数组与字符串转化

字符串转数组: toCharArray()

代码:

public class Demo {
    public static void main(String[] args) {
        String str=new String("hello world");
        char[] ch=str.toCharArray() ;
        int len=str.length();
        for (int i = 0; i < len; i++) {
            System.out.print(ch[i]);
        }
        System.out.println(); //hello world 
    }
}

数组转字符串

代码:

public class Demo {
    public static void main(String[] args) {
     char[] arr = {'a','b'};
        String s2=new String (arr) ;
        System.out.println(s2); //ab
    }
}

5)字符串格式化format()

代码:

public class Demo {
    public static void main(String[] args) {
      String s5= String.format("%d-%d-%d",2021,10,15);
      System.out.println(s5);//结果输出:2021-10-15

    }
}

下次分享字符串常量池~~哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值