详解String类(一)

本文详细介绍了Java中字符串的构造方法、对象比较(包括==和equals的区别)、字符串查找、类型转化(数字和字符串)、大小写转换、数组转换、格式化以及替换操作,强调了字符串的不可变性。
摘要由CSDN通过智能技术生成

目录

1.常用方法

1.1字符串构造

 注意

1.String是引用类型,内部并不存储字符串本身,在String类的实现源码中,String类实例变量如下:

1.2 String对象的比较

1.  ==比较是否引用同一个对象

2. 按照字典序比较(equals方法)

3. int compareTo(String s)方法

4.int compareToIgnoreCase(String s)方法:

1.3字符串查找

1.4转化

1.数值和字符串转化

2.大小写转换

3.字符串转数组

4.格式化

1.5 字符串替换


1.常用方法

1.1字符串构造

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

public static void main(String[] args) {
// 使用常量串构造
String s1 = "hello world";

// 直接newString对象
String s2 = new String("hello world");

// 使用字符数组进行构造
char[] array = {'h','e','l','l','o','w','o','r','l','d'};
String s3 = new String(array);
}

 注意

1.String是引用类型,内部并不存储字符串本身,在String类的实现源码中,String类实例变量如下:

public static void main(String[] args) {
// s1和s2引用的是不同对象  s1和s3引用的是同一对象
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1;

System.out.println(s1.length());  // 获取字符串长度---输出5
System.out.println(s1.isEmpty()); // 如果字符串长度为0 ,返回true ,否则返回false }

2.在Java中双引号引起来的也是String类型对象" "

// 打印"hello"字符串(String对象)的长度
System.out.println("hello".length());

1.2 String对象的比较

字符串的比较是常见操作之一,比如:字符串排序。Java中总共提供了4种方式:

1.  ==比较是否引用同一个对象

对于内置类型,==比较的是变量中的值,对于引用类型==比较的是引用中的地址。

public static void main(String[] args) {
    int a = 10;
    int b = 20;
    int c = 10;
// 对于基本类型变量 ,==比较两个变量中存储的值是否相同
System.out.println(a == b);   // false
System.out.println(a == c);   // true

// 对于引用类型变量 ,==比较两个引用变量引用的是否为同一个对象
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方法)

字典序:字符大小的顺序

String类重写了父类Objectequals方法, Objectequals默认按照==比较, String重写equals方法后,按照 如下规则进行比较,比如:s1.equals(s2)

public boolean equals(Object object) {
// 1. 先检测this 和 object 是否为同一个对象比较 ,如果是返回true
if (this == object) {
    return true;
    }


// 2. 检测object是否为String类型的对象 ,如果是继续比较 ,否则返回false
if (object instanceof String) {
// 将object向下转型为String类型对象
    String anotherString = (String)object;
    int n = value.length;
// 3. this和anObject两个字符串的长度是否相同 ,是继续比较 ,否则返回false
if (n == anotherString.value.length) {
    char v1[] = value;
    char v2[] = anotherString.value;
    int i = 0;
// 4. 按照字典序 ,从前往后逐个字符进行比较
    while (n-- != 0) {
    if (v1[i] != v2[i])
    return false;
    i++;
          }
        return true;
    }
}
return false;

3. int compareTo(String s)方法

equals不同的是,  equals返回的是boolean类型,而compareTo返回的是int类型。具体比较式:

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

 2. 如果前k个字符相等(k为两个字符串中长度最小值),返回值两个字符串长度差值

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));  // 不同输出字符差值-1
    System.out.println(s1.compareTo(s3));  // 相同输出 0
    System.out.println(s1.compareTo(s4));  // 前k个字符完全相同 ,输出长度差值 -3 
}

4.int compareToIgnoreCase(String s)方法:

与compareTo方法相同,但是忽略大小写比较

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));  // 不同输出字符差值-1
    System.out.println(s1.compareToIgnoreCase(s3));  // 无论大小写,相同输出 0
    System.out.println(s1.compareToIgnoreCase(s4));  // 前k个字符完全相同 ,输出长度差值 -3 
}

1.3字符串查找

String类提供的常用查找的方法:

方法功能
char charAt(int index)返回index位置上的字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常
int indexOf(int ch)返回ch第一次出现的位置,此处设计类型提升,实际使用时用单个字符即可

int indexOf

(int ch

int  beginIndex) 

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

int indexOf(String str

int beginIndex)

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

int lastIndexOf(int ch,

int beginIndex)

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

int lastIndexOf(String str

int beginIndex)

从beginIndex位置开始从后往前找str第一次出现的位置,没有则返回-1
public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";

System.out.println(s.charAt(3));                 //'b'

System.out.println(s.indexOf('c'));              //6

System.out.println(s.indexOf('c', 10));          //15

System.out.println(s.indexOf("bbb"));            //3

System.out.println(s.indexOf("bbb", 10));        //12 

System.out.println(s.lastIndexOf('c'));          //17

System.out.println(s.lastIndexOf('c', 10));      //8

System.out.println(s.lastIndexOf("bbb"));        //12

System.out.println(s.lastIndexOf("bbb", 10))     //3

注意:上述方法都是实例方法

1.4转化

1.数值和字符串转化

public static void main(String[] args) {
// 数字转字符串
    String s1 = String.valueOf(1234);
    String s2 = String.valueOf(12.34);
    String s3 = String.valueOf(true);
    String s4 = String.valueOf(new Student("Romised", 18));

// 字符串转数字
    int data1 = Integer.parseInt("1234");
    double data2 = Double.parseDouble("12.34");
}

2.大小写转换

public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";

// 小写转大写
System.out.println(s1.toUpperCase());

// 大写转小写
System.out.println(s2.toLowerCase());

}    

3.字符串转数组

public static void main(String[] args) {
     String s = "hello";
// 字符串转数组
    char[] ch = s.toCharArray();
    for (int i = 0; i < ch.length; i++) {
    System.out.print(ch[i]);
 }
    System.out.println();//hello
// 数组转字符串
    String s2 = new String(ch); 
    System.out.println(s2);//hello
}

4.格式化

public static void main(String[] args) {
    String s = String.format("%d-%d-%d", 2023, 11,30);
    System.out.println(s);//2023-11-30
}

1.5 字符串替换

使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:

String replaceAll(String regex, String replacement)

替换所有的指定内容

String replaceFirst(String regex, String replacement)

替换首个内容

代码示例: 字符串的替换处理

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

注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值