String类

简介: String类

一、String类
在C语言中要表示字符串只能使用字符数组或者字符指针,在java中专门提供了String类.

  1. String初始化

1.常量串构造

public static void main(String[] args) {

    String str1 = "dachang";
}

1
2
3
2.new String对象

public static void main(String[] args) {

    String str2 = new String("dachang");
}

1
2
3
3.使用字符数组构造

public static void main(String[] args) {

    char[] ch = {'d','a','c','h','a','n','g'};
    String str3 = new String(ch);
}

1
2
3
4

  1. String具体存储形式

String是引用类型,内部不存储字符串本身,而是存储的一个地址.我们打开String的源码看一下

字符串由两部分组成char[ ]和hash两部分组成,String实际保存在char数组中.

public static void main(String[] args) {

    String s1 = new String("dachang");
    String s2 = new String("woyao");
    String s3 = s1;
}

1
2
3
4
5
我们具体看一下在堆栈上是如何存储的.

二、String中的比较
==比较是否引用同一个对象
public static void main(String[] args) {

    String s1 = new String("dachang");
    String s2 = new String("dachang");
    System.out.println(s1 == s2);
}

1
2
3
4
5

2.equals方法.按照具体字符串内容比较.

我们可以发现Object中的equals方法是按照==方式,比较的是引用地址.

String类中重写了Object中的equals方法,按照字符串中的内容比较.

public static void main(String[] args) {

    String s1 = new String("dachang");
    String s2 = new String("dachang");
    System.out.println(s1.equals(s2));
}

1
2
3
4
5

  1. compareTo方法.

equals只能比较两个字符串是否相等,返回一个boolean类型.但如果你要知道谁大谁小这时候equals就不能满足了.

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

我们发现String类是实现了Comparable接口的.
public static void main(String[] args) {

    String s1 = new String("abc");
    String s2 = new String("abb");
    System.out.println(s1.compareTo(s2));
}

1
2
3
4
5

忽略大小写的比较:
compareToIgnoreCase方法与comparaTo相同但忽略大小写.

public static void main(String[] args) {

    String s1 = new String("abc");
    String s2 = new String("ABC");
    System.out.println(s1.compareToIgnoreCase(s2));
}

1
2
3
4
5

三、字符串查找

  1. charAt(int index)

public static void main(String[] args) {

    String str = new String("hello");
    System.out.println(str.charAt(2));
}

1
2
3
4
charAt()方法是返回index下标的字符

2.indexOf(int ch)

public static void main(String[] args) {

    String str = new String("hello");
    System.out.println(str.indexOf('l'));
}

1
2
3
4
indexOf()方法是返回ch字符第一次出现的位置,没有返回-1

3.indexOf(String str)

public static void main(String[] args) {

    String str = new String("hello");
    System.out.println(str.indexOf("el"));
}

1
2
3
4
indexOf()方法返回str字符串第一次出现的位置,没有返回-1

4.lastIndex(int ch)

public static void main(String[] args) {

    String str = new String("hello");
    System.out.println(str.lastIndexOf('l'));
}

1
2
3
4
lastindexOf()方法是从后往前找返回ch字符第一次出现的位置,没有返回-1

5.lastIndex(String str)

public static void main(String[] args) {

    String str = new String("hello");
    System.out.println(str.lastIndexOf("lo"));
}

1
2
3
4
lastindexOf()方法是从后往前找返回str字符串第一次出现的位置,没有返回-1

四、字符串转化

  1. 大小写转化

toUpperCase()小写转大写,其他字符不变

public static void main(String[] args) {

    String str = new String("hello");
    System.out.println(str.toUpperCase());
}

1
2
3
4

toLowerCase()小写转大写,其他字符不变

public static void main(String[] args) {

    String str = new String("HELLO");
    System.out.println(str.toLowerCase());
}

1
2
3
4

  1. 数值字符串相互转化

数值转字符串

class Person{

public String name;
public int age;

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

@Override
public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            '}';
}

}

public static void main(String[] args) {
    String s1 = String.valueOf(100);
    String s2 = String.valueOf(10.5);
    String s3 = String.valueOf(true);
    String s4 = String.valueOf(new Person("zhangsan",21));
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(s3);
    System.out.println(s4);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
实例类型转字符时需要重写toString()方法,不然输出的时路径@哈希地址

字符串转数值

public static void main(String[] args) {

    int a = Integer.parseInt("110");
    double b = Double.parseDouble("99.5");
    System.out.println(a);
    System.out.println(b);
}

1
2
3
4
5
6
这里使用到了Integer包装类型.

相关文章
|
26天前
|
Java 安全 索引
滚雪球学Java(48):面向对象编程中的StringBuffer类详解
【6月更文挑战第2天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
31 5
滚雪球学Java(48):面向对象编程中的StringBuffer类详解
|
28天前
|
存储 Java 测试技术
滚雪球学Java(47):String类教程:如何在Java中使用字符串操作
【6月更文挑战第1天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
24 2
滚雪球学Java(47):String类教程:如何在Java中使用字符串操作
|
7天前
|
Java API 索引
java中String类常用API
java中String类常用API
|
1天前
|
存储 Java API
Java基础之String类
Java的String类是引用类型,用于创建和操作字符串。字符串对象在`java.lang`包中,不可变。创建方式包括字面量和`new`关键字。字符串池存储字符串常量,避免重复。比较字符串用`equals()`(区分大小写)和`equalsIgnoreCase()`(不区分大小写)。`length()`返回长度,`concat()`或`+`拼接,`substring()`截取,`indexOf()`和`lastIndexOf()`查找,`replace()`替换,`split()`分割。这些是常用的字符串API。
7 0
|
1天前
|
Java
Java基础之String类
Java基础之String类
8 0
|
23天前
|
C语言 C++
C++初阶学习第六弹——探索STL奥秘(一)——标准库中的string类
C++初阶学习第六弹——探索STL奥秘(一)——标准库中的string类
16 0
|
25天前
|
算法 Linux C语言
7.学习STL和string类:版本、组件、构造、操作及应用
7.学习STL和string类:版本、组件、构造、操作及应用
|
3天前
|
存储
MyString:string类的模拟实现
MyString:string类的模拟实现
|
3天前
string类重要接口(2):`+=`
string类重要接口(2):`+=`
|
3天前
string类:`reserve()`,`resize()`详解
string类:`reserve()`,`resize()`详解