
在C语言中要表示字符串只能使用字符数组或者字符指针,在java中专门提供了String类.
1.常量串构造
public static void main(String[] args) {
String str1 = "dachang";
}
2.new String对象
public static void main(String[] args) {
String str2 = new String("dachang");
}
3.使用字符数组构造
public static void main(String[] args) {
char[] ch = {'d','a','c','h','a','n','g'};
String str3 = new String(ch);
}
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;
}
我们具体看一下在堆栈上是如何存储的.

public static void main(String[] args) {
String s1 = new String("dachang");
String s2 = new String("dachang");
System.out.println(s1 == s2);
}

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));
}

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

public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("abb");
System.out.println(s1.compareTo(s2));
}

忽略大小写的比较:
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. charAt(int index)
public static void main(String[] args) {
String str = new String("hello");
System.out.println(str.charAt(2));
}
charAt()方法是返回index下标的字符

2.indexOf(int ch)
public static void main(String[] args) {
String str = new String("hello");
System.out.println(str.indexOf('l'));
}
indexOf()方法是返回ch字符第一次出现的位置,没有返回-1

3.indexOf(String str)
public static void main(String[] args) {
String str = new String("hello");
System.out.println(str.indexOf("el"));
}
indexOf()方法返回str字符串第一次出现的位置,没有返回-1

4.lastIndex(int ch)
public static void main(String[] args) {
String str = new String("hello");
System.out.println(str.lastIndexOf('l'));
}
lastindexOf()方法是从后往前找返回ch字符第一次出现的位置,没有返回-1

5.lastIndex(String str)
public static void main(String[] args) {
String str = new String("hello");
System.out.println(str.lastIndexOf("lo"));
}
lastindexOf()方法是从后往前找返回str字符串第一次出现的位置,没有返回-1

toUpperCase()小写转大写,其他字符不变
public static void main(String[] args) {
String str = new String("hello");
System.out.println(str.toUpperCase());
}

toLowerCase()小写转大写,其他字符不变
public static void main(String[] args) {
String str = new String("HELLO");
System.out.println(str.toLowerCase());
}

数值转字符串
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);
}
实例类型转字符时需要重写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);
}
这里使用到了Integer包装类型.

public static void main(String[] args) {
String str = new String("abcd");
char[] ch = str.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]+" ");
}
}

public static void main(String[] args) {
String str = String.format("%d-%d-%d",2022,8,31);
System.out.println(str);
}

去掉字符串左右所有的空格会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等
public static void main(String[] args) {
String str = " hello ";
System.out.println(str.trim());
}

传入一个参数,是截取此位置到结尾.
public static void main(String[] args) {
String str = "hello world!";
System.out.println(str.substring(6));
}

传入两个参数,是截取指定范围内容
public static void main(String[] args) {
String str = "woyaojindachang";
System.out.println(str.substring(2,8));
}

使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:
public static void main(String[] args) {
String str = new String("2022.8.31");
System.out.println(str.replace('.','-'));
}

如果只想替换第一个内容使用replaceFirst()即可
| 方法 | 功能 |
|---|---|
| String[] split(String regex) | 字符串全部拆分 |
| String[] split(String regex, int limit) | 将字符串以指定的格式,拆分为limit组 |
public static void main(String[] args) {
String str = "wo yao jin da chang";
String[] arr = str.split(" ");
for (String s: arr) {
System.out.println(s);
}
}

public static void main(String[] args) {
String str = "wo yao jin da chang";
String[] arr = str.split(" ",2);
for (String s: arr) {
System.out.println(s);
}
}

特殊情况的拆分:
1.如果一个字符串中有多个分隔符,可以用"|"作为连字符.
public static void main(String[] args) {
String str = "wo&jin=da=chang";
String[] arr = str.split("=|&");
for (String s:arr) {
System.out.println(s);
}
}

2. 字符"|“,”*“,”+"都得加上转义字符,前面加上 “\”
public static void main(String[] args) {
String str = "2002.8.31";
String[] s = str.split("\\.");
for (String ss:s) {
System.out.println(ss);
}
}

3. 如果是 “” ,那么就得写成 “\\” .
public static void main(String[] args) {
String str = "2002\\8\\31";
String[] s = str.split("\\\\");
for (String ss:s) {
System.out.println(ss);
}
}
