1.String

String概述

String类代表字符串,Java程序中的所有字符串文字都被实现为此类的实例,也就是说Java程序中所有的双引号字符串,都是String类的对象。

字符串特点:

  • 字符串不可变,它们的值在创建后不能被更改;
  • 虽然String 的值是不可变的,但是可以被共享;
  • 字符串效果上相当于字符数组(char []),但是底层原理是字节数组(byte []);

1.1String构造方法

方法名

说明

public String()

创建一个空白字符串对象,不含有任何内容

public String(char[] chs)

根据字符数组的内容,来创建字符串对象

public String(byte[] bys)

根据字节数组的内容,来创建字符串对象

String str = “abc”

直接赋值的方式创建字符串对象,内容就是abc**(常用)**

Test01:

package com.wan.api;
public class Test01 {
    public static void main(String[] args) {
        //public String()创建一个空白字符串对象,不含有任何内容
        String str1 = new String();
        System.out.println("str1="+str1);

        //public String(char[] chs)根据字符数组的内容,来创建字符串对象
        char[] ch = {'a', 'b', 'c'};
        String str2 = new String(ch);
        System.out.println("str2="+str2);

        //public String(byte[] bys)根据字节数组的内容,来创建字符串对象
        byte[] bys = {97,98,99};
        String str3 = new String(bys);
        System.out.println("str3="+str3);

        //直接赋值的方式创建字符串对象(推荐)
        String str4 = "abc";
        System.out.println("str4="+str4);
    }
}

运行结果:

str1=
str2=abc
str3=abc
str4=abc

1.2String对象的特点

  • 通过new创建的字符串对象,每一次new都会申请一个内存空间,虽然内容相同,但是地址不同;
  • 以""给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,都只会建立一个String对象;

Test02:

package com.wan.api;

public class Test02 {
    public static void main(String[] args) {
        char[] ch = {'a', 'b', 'c'};
        String s1 = new String(ch);
        String s2 = new String(ch);
        String s3 = "abc";
        String s4 = "abc";

        //比较字符串地址是否相同
        System.out.println(s1 == s2);
        System.out.println(s3 == s4);
        System.out.println("================");
        //比较字符串内容是否相同
        System.out.println(s1.equals(s2));
        System.out.println(s3.equals(s4));
        System.out.println("=================");
        System.out.println("s1="+s1);
        System.out.println("s2="+s2);
        System.out.println("s3="+s3);
        System.out.println("s4="+s4);
    }

}

运行结果:

false
true
================
true
true
=================
s1=abc
s2=abc
s3=abc
s4=abc

1.3String类常用方法

  • 基本

方法名

说明

public boolean equals(Object anObject)

将此字符串与指定对象进行比较

public int length()

返回字符串中所包含的字符数量(字符串长度)

public char charAt(int index)

返回指定索引位置的字符,注意字符串中第一个字符索引是0,最后一个是length()-1

public String contact(String str)

将参数中的字符串str连接到当前字符串的后面

Test03:

package com.wan.api;

public class Test03 {
    public static void main(String[] args) {
        String s1="abc";
        String s2="abc";
        String s3="晚风花间寺中人";
        String s4="关注+晚风花间寺中人-->";
        String s5=
       // public boolean equals(Object anObject) | 将此字符串与指定对象进行比较
        boolean e1 = s1.equals(s2);
        System.out.println(s1.equals(s2));
        //  public int length()   | 返回字符串中所包含的字符数量(字符串长度)
        s2.length();
        System.out.println(s2.length());
        // public char charAt(int index)  | 返回指定索引位置的字符,注意字符串中第一个字符索引是0,最后一个是length()-1
        s3.charAt(3);
        System.out.println(s3);
        //public String contact(String str)      | 将参数中的字符串str连接到当前字符串的后面
        s4.concat(s5);
        System.out.println(s4.concat(s5));


    }

}

运行结果:

true
3
晚风花间寺中人
关注+晚风花间寺中人
  • 提取

方法名

说明

public String substring(int beginIndex)

从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回

public String substring(int beginIndex,int endIndex)

从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回

public CharSequence subSequence(int beginIndex, int endIndex)

返回一个字符序列,该序列是该序列的子序列。

Test04:

package com.wan.api;

public class Test04 {

    public static void main(String[] args) {
        String s1="abc";
        String s2="abc";
        String s3="晚风花间寺中人";
        String s4="关注+晚风花间寺中人-->";
        String s5=";
        //public String substring(int beginIndex)从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回
        System.out.println(s5.substring(4));
        //从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回
        System.out.println(s4.substring(0,10));
//        public CharSequence subSequence(int beginIndex,
//        int endIndex)返回一个字符序列,该序列是该序列的子序列。
//        调用此方法的形式 :
//        str.subSequence(begin, end)行为与调用完全相同
//        str.substring(begin, end)
        System.out.println(s5.subSequence(0, 4));

    }
}

运行结果:

博客
关注+晚风花间寺中人
  • 查找

方法名

说明

public int indexOf(int ch/String str)

用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1

public int indexOf(int ch/String str,int fromIndex)

用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从指定的索引起首次出现的位置,若没有出现则返回-1

public int lastIndexOf(int ch/String str)

用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起最后一次出现的位置,若没有出现则返回-1

public int lastIndexOf(int ch/String str,int fromIndex)

用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从指定的索引向后搜索最后一次出现的位置,若没有出现则返回-1

Test05:

package com.wan.api;

public class Test05 {
    public static void main(String[] args) {
        String str = "I love China, i am proud of my country";
        //public int indexOf(int ch/String str)
        //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1
        System.out.println("1-->  "+str.indexOf('a'));
        System.out.println("2-->  "+str.indexOf("am"));
        //public int indexOf(int ch/String str,int fromIndex)
        //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从指定的索引起首次出现的位置,若没有出现则返回-1
        System.out.println("3-->  "+str.indexOf('o',4));
        System.out.println("4-->  "+str.indexOf("proud",6));
        //public int lastIndexOf(int ch/String str)
        //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起最后一次出现的位置,若没有出现则返回-1
        System.out.println("5-->  "+str.lastIndexOf('y'));
        System.out.println("6-->  "+str.lastIndexOf("of"));
        //public int lastIndexOf(int ch/String str,int fromIndex)
        //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从指定的索引向后搜索最后一次出现的位置,若没有出现则返回-1
        System.out.println("7-->  "+str.lastIndexOf('o',20));
        System.out.println("8-->  "+str.lastIndexOf("am",9));

    }
}

运行结果:

1-->  11
2-->  16
3-->  21
4-->  19
5-->  37
6-->  25
7-->  3
8-->  -1
  • 替换

方法名

说明

public String replace(char oldChar,char newChar)

用字符串newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串

public String replaceFirst(String regex,String replacement)

用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,将新的字符串返回

public String replaceAll(String regex,String replacement)

用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,将新的字符串返回

public String replace(CharSequence target,CharSequence replacement)

将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列

Test06:

package com.wan.api;

public class Test06 {

    public static void main(String[] args) {
        String str = "I love China, i am proud of my country";
        System.out.println("str= "+str);
        //public String replace(char oldChar,char newChar)
        //用字符串newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串
        System.out.println(str.replace(',','!'));

        //public String replaceFirst(String regex,String replacement)
        //用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,将新的字符串返回
        System.out.println(str.replaceFirst("love","like"));

        //public String replaceAll(String regex,String replacement)
        //用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,将新的字符串返回
        System.out.println(str.replaceAll("country","hometown"));
    }


}

运行结果:

str= I love China, i am proud of my country
I love China! i am proud of my country
I like China, i am proud of my country
I love China, i am proud of my hometown
  • 转换

方法名

说明

public static String valueOf(char[] data)

返回char数组参数的的字符串char形式

public static String valueOf(char data[],int offset,int count)

返回char数组参数的特定子阵列的字符串char形式

public static String valueOf(boolean b)

返回boolean参数的字符串boolean形式

public static String valueOf(char c)

返回char参数的字符串char形式

public static String valueOf(int i)

返回int参数的字符串int形式

public static String valueOf(long l)

返回long参数的字符串long形式

public static String valueOf(float f)

返回float参数的字符串float形式

public static String valueOf(double d)

返回long参数的字符串doubel形式

Test07:

package com.wan.api;

public class Test07 {
    public static void main(String[] args) {
        char[]data={'A','B','C'};
        String str = "I love China, i am proud of my country";
        int i=2020;
        //public static String valueOf(char[] data)   | 返回char数组参数的的字符串char形式
        String s1 =  String.valueOf('A');
        System.out.println(s1);
        //public static String valueOf(char data[],int offset,int count) | 返回char数组参数的特定子阵列的字符串char形式 |
        String s2 = String.valueOf(new char[]{'A'},0,1);
        System.out.println(s2);
        // public static String valueOf(boolean b) | 返回boolean参数的字符串boolean形式
        String s3 = String.valueOf(true);
        System.out.println(s3);
        // public static String valueOf(char c)  | 返回char参数的字符串char形式
        String s4 =String.valueOf('C');
        System.out.println(s4);
        // public static String valueOf(int i)    | 返回int参数的字符串int形式
        String s5 = String.valueOf(i);
        System.out.println(s5);
        System.out.println(str);
        // public static String valueOf(long l)    | 返回long参数的字符串long形式
        String s6 = String.valueOf(12);
        System.out.println(s6);
        // public static String valueOf(float f)    | 返回float参数的字符串float形式
        String s7 = String.valueOf(32.9);
        System.out.println(s7);
        //public static String valueOf(double d)      | 返回long参数的字符串double形式
        String s8 =String.valueOf(66.99);
        System.out.println(s8);



    }


}

运行结果:

A
A
true
C
2020
I love China, i am proud of my country
12
32.9
66.99
  • 格式化

方法名

说明

public static format(Locale l,String format,Object… args)

使用指定的区域设置,格式字符串和参数返回格式化字符串

public static format(String format,Object… args)

使用指定的格式字符串和参数返回格式化的字符串

转换符

详细说明

示例

%s

字符串类型

“helloword”

%c

字符类型

‘y’

%b

布尔类型

true/false

%d

整数类型

88(十进制)

%x

整数类型

FF(十六进制)

%o

整数类型

77(八进制)

%f

浮点数类型

6.66

%a

十六进制浮点数类型

FF.35AE

%e

指数类型

9.21e+5

%g

通用浮点类型(f和e类型中较短的)

基本用不到

%h

散列码

基本用不到

%%

百分比类型

%(%特殊字符%%才能显示%)

%n

换行符

基本用不到

%tx

日期与时间类型(x代表不同的日期与时间转换符)

基本用不到

搭配转换符

标志

说明

示例

结果

+

为正数或负数添加符号

("%+d",9)

+9

0

数字前补0

("%02",66)

0066

空格

在整数前添加指定数量空格

("% 4d",66)

66


以","对数字分组

("%,f",6666.66)

66,666,660000


使用括号包含负数

("%(f",-66.66)

(66.660000)

#

如果是浮点数则包含小数点,如果是16进制或8进制则添加0x或0

("%#x",99)

0x63

<

格式化前一个转换符所描述的参数

("%f和%❤️.2f",66.45)

66.450000和66.45

$

被格式化的参数索引

("%1s",66,“abc”)

66,abc