java-String类

本文详细讲解了Java中字符串的构造方式、引用类型特性,比较方法(equals,compareTo,compareToIgnoreCase),查找功能(charAt,indexOf,lastIndexOf),以及数字和字符串之间的转化。还介绍了StringBuilder和StringBuffer的使用及其与String的区别。
摘要由CSDN通过智能技术生成

1,字符串构造

分为三种方法:

String s1 = "hello world";

String s2 = new String("hello world");

char[] array = {'h','e','l','l','o'}; String s3 = new String(array)

String为引用类型,存储的并不是字符本身。

String s1 = new String("hello");

String s2 = new String("world");

String s3 = s1;

s1,和s2指向的是不同的对象,这时s3 = s1,将s1的对象的地址赋值给s3,则s3存储的也是s1的对象的地址,所以s1和s3指向同一个对象。

2,String对象的比较

1,基本类型变量

int a = 10; int b = 20; int c = 10;

System.out.println(a == b);  System.out.println(a == c);

前者为false,后者为true。因为int为基本类型变量,比较的为变量存储的值是否相同

2,,String引用类型变量

String为引用类型变量,比较两个引用变量引用的是否为同一个对象

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

3,equals比较

如果用重写的equals进行比较(在JAVA接口详解详细介绍了),则

System.out.println(s1.equals(s2));  true

System.out.println(s1.equals(s3));  false

4,int compareTo(String s) 方法

1,按照字典次序大小比较,如果出现不相同的字符,则直接返回这两个字符的大小差值

2,如果前k个字符相等(k为两字符串中较短字符串的长度),返回两字符串的长度差值(为负数)

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)); -1
System.out.println(s1.compareTo(s3));  0
System.out.println(s1.compareTo(s4));  -3

5, int compareToIgnoreCase(String str) 方法

compareToIgnoreCase与compareTo方法类似,只是compareToIgnoreCase比较时忽略字符大小写。

String s1 = new String("abc");
String s3 = new String("ABc");
System.out.println(s1.compareToIgnoreCase(s3));//0

3,字符串查找

(1)char charAt(int index):返回index位置上字符,如果index为负数或者越界,抛出 IndexOutOfBoundsException异常

String arr="abcdefg";
System.out.println(arr.charAt(2));
System.out.println(arr.charAt(10));

(2)int  indexOf (int ch):返回ch第一次出现的位置,没有返回-1

String arr="abcdefg"; 
System.out.println(arr.indexOf('f'));//5
System.out.println(arr.indexOf('z'));//-1

(3)int indexOf(int ch, int fromIndex):从fromIndex位置开始找ch第一次出现的位置,没有返回-1

String arr="abcdefg";
System.out.println(arr.indexOf('d', 1));//3
System.out.println(arr.indexOf('d', 4));//-1

(4)int indexOf(String str):返回str第一次出现的位置,没有返回-1

String arr="abcdefgabcdef";
System.out.println(arr.indexOf("ef"));//4

(5)int indexOf(String str, int fromIndex):从fromIndex位置开始找str第一次出现的位置,没有返回-1

String arr="abcdefgabcdef";
System.out.println(arr.indexOf("ef", 8));//11

(6)int lastIndexOf(int ch):从后往前找,返回ch第一次出现的位置,没有返回-1

String arr="abcdefgabcdef";
System.out.println(arr.lastIndexOf('a'));//7

(7)int lastIndexOf(int ch, int fromIndex):从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返 回-1

String arr="abcdefgabcdef";
System.out.println(arr.lastIndexOf('d', 12))//10

(8)int lastIndexOf(String str):从后往前找,返回str第一次出现的位置,没有返回-1

String arr="abcdefgabcdef";
System.out.println(arr.lastIndexOf("de"));//10

(9)int lastIndexOf(String str, int fromIndex):从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1

String arr="abcdefgabcdef";
System.out.println(arr.lastIndexOf("de",6));//3

4,转化

(1)数字转字符串: String.valueOf();

String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
System.out.println(s1);//1234
System.out.println(s2);//12.34
System.out.println(s3);//true

(2)将字符串转化成数字

int a = Integer.parseInt("1234");
double b = Double.parseDouble("12.34");
System.out.println(a);
System.out.println(b);

(3)大小写转化

1,小写转大写:toUpperCase()

2,大写转小写:toLowerCase()

public static void main(String[] args) {
        String s1 = "Hello";
        String s2 = "HELLo";
        System.out.println(s1.toUpperCase());//HELLO
        System.out.println(s2.toLowerCase());//hello
    }

(4)字符串转数组:toCharArray()

 String s = "hello";
        char[] ch = s.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
        System.out.println();

(5)格式化:format()

String ss = String.format("%d-%d-%d", 2024,3,26);
System.out.println(ss);2024-3-26

5,字符串替换

String replaceAll(String regex, String replacement);

String replaceFirst(String regex, String replacement)

String str = "helloworld" ;
    System.out.println(str.replaceAll("l", "_"));
    System.out.println(str.replaceFirst("l", "_"));
he__owor_d
he_loworld

6,字符串拆分

String[] split(String regex):其中regex为拆分依据

String[] split(String regex, int limit):limit为限制拆分的组数

String str = "hello world" ;
String[] result = str.split(" ") ;  按照空格拆分
for(String s: result) {
    System.out.println(s);
}

hello
world

String str = "hello world hello world" ;
String[] result = str.split(" ",2) ;
for(String s: result) {
    System.out.println(s);
}

hello
world hello world

注意:

1. 字符"|","*","+"都得加上转义字符,前面加上 "\\" .

2. 而如果是 "\" ,那么就得写成 "\\\\" .

String str = "name=zhangsan&age=18" ;
String[] result = str.split("&") ;
for (int i = 0; i < result.length; i++) {
    String[] temp = result[i].split("=") ;
    System.out.println(temp[0]+" = "+temp[1]);
}

name = zhangsan
age = 18
 

7,字符串截取

String substring(int beginIndex):从指定索引截取到结尾

String substring(int beginIndex, int endIndex):截取部分内容

String str = "helloworld" ;
System.out.println(str.substring(5));//world
System.out.println(str.substring(0, 5));//hello

注意:str.substring(0, 5),为 [0,5)

8,字符串的不可变性

String str=“abc“;

String str=“ABC“;相当于产生了全新的对象,存放的为“ABC”的地址

我们通过数组来类比字符串,探求字符串的不可变性

int [] array=new int []{1,2,3};
array=new int[]{4,5,6};将另一个数组的地址赋值给array(可以运行)
array[0]=99;改变array数组中的值(可以运行
final int [] array=new int []{1,2,3};
array=new int[]{4,5,6};(报错)
array[0]=99;(可以运行)
->说明了array指向的value的值是不能发生改变的,而value指向的对象中的内容可以发生改变
->也就是说只要拿到String对象的引用,就可以改变其中的内容

但是在String的原生代码,是这样定义String的<private final byte[ ] value>。final修饰使String所指value不能改变,而private修饰,则在类外无法拿到value这个引用。综上所述,字符串不能被改变

9,字符串修改

注意:尽量避免直接对String类型对象进行修改,因为String类是不能修改的,所有的修改都会创建新对象,效率非常低下。

例如:

String str=new String("abc");问:在不考虑常量池的情况下 ,创建了几个对象。2个
String str=new String("a")+new String("b");
问:在不考虑常量池的情况下 ,创建了几个对象。答最少6个(4+“a”和“b”’的拼接+在创建一个对象把拼接好的放进去=6个)

10,StringBuilder和StringBuffer

由于String的不可更改特性,为了方便字符串的修改,Java中又提供StringBuilder和StringBuffer类。

String str="hello";
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append(str);
stringBuilder.append("world");
str=stringBuilder.toString();
System.out.println(str);

注意:String和StringBuilder类不能直接转换。如果要想互相转换,可以采用如下原则:

String变为StringBuilder: 利用StringBuilder的构造方法或append()方法

StringBuilder变为String: 调用toString()方法。

我们通过实例化一个对象直接修改或者对象里面的内容,这样可以有效的防止创建多个对象。

两者的区别在于Stringbuffer线路安全,适用于多线路问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值