java的String类(字符串)

String类

Java中,String是一个引用类型,它本身也是一个class
Java字符串的一个重要特点就是字符串不可变。这种不可变性是通过内部的private final char[]字段,以及没有任何修改char[] 的方法实现的。

1、创建字符串:

1、最普通创建方式

String str_name = "String_test1"

2、使用构造函数创建字符串方式

String str_name = new String("String_test2")

3、将数组转换成字符串

import java.util.Arrays;

public class String_test {
    public static void main(String[] args) {
        char[] arg={'a','b','c','d','e'};
        String argString = new String(arg);//将数组内容转换成字符串
        
        String argString2=Arrays.toString(arg);//将整个数组转化成字符串
        
        System.out.println(argString);
        System.out.println(argString2);
    }
}

空字符串和null是两个概念,一个空字符串不代表其内容为null,他是实例化后的对象,但是不包含任何值。

2、字符串长度

用于获取有关对象的信息的方法称为访问器方法。
String 类的一个访问器方法是 length() 方法,它返回字符串对象包含的字符数。

String_name.length()

例如:

public class String_test {
    public static void main(String[] args) {
        String str = "javaSE";
        System.out.println(str.length());
    }
}

3、字符串的连接

1、Stringconcat() 方法链接

string1.concat(string2);

2、最常用普通 “+” 号链接

string1+string2

例如:

public class String_test {
    public static void main(String[] args) {
        String str1 = "javaSE";
        String str2 = "study";
        System.out.println(str1.concat(str2));//使用concat()链接
        System.out.println(str1+str2);//使用“+”号链接
    }
}

4、字符串的比较

  1. equals():用于比较两个字符串是否相等。
  2. equalIgnoreCase():与equals()方法功能相同,但是不区分大小写。
  3. startWith()endWith():用来判断字符串是否以指定的字符串开始或结束。
  4. compareTo():该字符串的大小大于、等于还是小于另一个字符串的大小,依据是他们在字典中的顺序。

比较两个字符串是否相同时,实际上是比较字符串的内容是否相同。必须使用*equals()*方法而不能用==。

//equals()和equalsIgnoreCase()
public class String_test {
    public static void main(String[] args) {
        String str1 = "javaSE";
        String str2 = "study";
        String str3 = "javaSE";
        String str4 = "STUDY".toLowerCase();//将大写变小写
        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));
        System.out.println(str1==str3);
        System.out.println(str2.equals(str4));
        System.out.println(str2==str4);
        System.out.println(str2.equalsIgnoreCase(str4));//equalsIgnoreCase
    }
}

false
true
true
true
false
true

//startsWith()和endWith()
public class String_test {
    public static void main(String[] args) {
        String str1 = "ABCDE";
        System.out.println(str1.startsWith("a"));
        System.out.println(str1.endsWith("DE"));
        System.out.println(str1.startsWith("CD",2));//判断指定位置开始是否为指定字符串。
        System.out.println(str1.startsWith("CE",2));
    }
}

false
true
true
false

//compareTo()
public class String_test {
    public static void main(String[] args) {
        String str1 = "ABCDE";
        String str2 = "BCDEF";
        String str3 = "A";
        String str4 = "B";
        System.out.println(str1.compareTo(str2));
        System.out.println(str2.compareTo(str1));
        System.out.println(str3.compareTo(str4));
        System.out.println(str4.compareTo(str3));
        System.out.println(str4.compareTo(str1));
        System.out.println(str1.compareTo(str4));
    }
}
//compare()方法的入口参数比较字符串对象,返回int值,如果相等返回0,如果大于返回正数,如果小于返回负数。

-1
1
-1
1
1
-1

5、字符串大小写转换

toUpperCase()toLowerCase()

public class String_test {
    public static void main(String[] args) {
        String str = "AbcDefGHijKLMn";
        System.out.println(str.toUpperCase());
        System.out.println(str.toLowerCase());
    }
}

ABCDEFGHIJKLMN
abcdefghijklmn

6、字符串的查找

Strring类提供了两种查找字符串的方法,允许在字符串中搜索指定的字符或字符串,indexOf() 方法用于搜索字符或字符串首次出现的位置,lastIndexOf() 方法用于搜索字符或字符串最后一次出现的位置。这两种方法均有重载,他们的返回值均为字符或字符串被发现的索引位置。如果未搜索到,则返回-1.

indexOf(int ch)//用于指定字符串中第一次出现的索引位置。int ch是指字符的编码值
indexOf(int ch)//用于指定字符串中最后一次出现的索引位置。
indexOf(int ch,int startIndex)//用于获取字符串在源字符串中指定索引位置开始第一次出现的索引位置。
indexOf(int ch,int startIndex)//用于获取字符串在源字符串中指定索引位置开始最后一次出现的索引位置。

indexOf(String str)//用于指定字符串中第一次出现的索引位置。
indexOf(String str)//用于指定字符串中最后一次出现的索引位置。
indexOf(String str,int startIndex)//用于获取字符串在源字符串中指定索引位置开始第一次出现的索引位置。
indexOf(String str,int startIndex)//用于获取字符串在源字符串中指定索引位置开始最后一次出现的索引位置。
public class String_test {
    public static void main(String[] args) {
        String str1 = "AbcdEfghijkABcdecedeA";
        System.out.println(str1.indexOf("A"));
        System.out.println(str1.lastIndexOf("b"));
        System.out.println(str1.indexOf(65));//A
        System.out.println(str1.indexOf("A",4));//第5个字符后的第一次出现A
        System.out.println(str1.lastIndexOf("c",3));
    }
}

7、从字符串中截取字符串

可以使用 substring() 方法从现有字符串中截取

substring(int biginIndex );//截取从指定索引位置到最后的字符串,截取得到的字符串包含指定索引位置的字符。
substring(int biginIndex , int end endIndex);//截取从指定位置开始到指定位置结束的字符串,截取得到的字符串包含开始不包含结束。
public class String_test {
    public static void main(String[] args) {
        String str1 = "AbcdEfghijkABcdecedeA";
        System.out.println(str1.substring(5));
        System.out.println(str1.substring(5,10));
    }
}

fghijkABcdecedeA
fghij

8、去掉字符串的首尾空格

trim():去掉字符串的首尾空格,得到新的字符串

public class String_test {
    public static void main(String[] args) {
        String str1 = "   java    ";
        System.out.println(str1.trim());
    }
}

java

9、替换字符串中的字符或子串

replace() 方法可以将原字符串的某个字符替换成指定的字符,并得到一个新的字符串。
replaceAll() 方法可以替换字符串,得到新的字符串。
replaceFirst 方法只替换原字符串中的第一个字符串。

replace(char oldChar , char newChar);
replaceAll(String regex , String replacement);
replaceFirst(String regex , String replacement);
public class String_test {
    public static void main(String[] args) {
        String str1 = "ABCD_ABCD_ABCD";
        System.out.println(str1.replace("A","D"));
        System.out.println(str1.replaceAll("ABCD","DEFG"));
        System.out.println(str1.replace("AB","DV"));//replace的重载方法,可以替换字符串。
        System.out.println(str1.replaceFirst("ABCD","DE"));
    }
}

DBCD_DBCD_DBCD
DEFG_DEFG_DEFG
DVCD_DVCD_DVCD
DE_ABCD_ABCD

10、分割字符串

split() 用来将字符串按照指定的规则进行分割,并以String型数组的方式返回,分割得到的字串按照他们在字符串中的顺序排序。

split(String regex , int limit);

split() 的第一个入口参数regex为分割规则,第二个入口参数limit用来设置分割规则的应用次数,所以将影响返回结果数组的长度。
1、如果limit>0,则分割规则将被应用(limit-1)次,数组的长度也不会大于limit,并且数组的最后一项将包含超出最后匹配的所有字符;
2、如果limit<0,则分割规则将被应用尽可能多的次数,并且数组可以是任意长度。
3、如果limit=0,分割规则讲将被应用尽可能多的次数,并且数组可以是任意长,且数组中最后一位的所有空字符串将会被丢弃。

import java.util.Arrays;

public class String_test {
    public static void main(String[] args) {
        String str = "boo_and_foo";
        String[] a=str.split("_",2);//分割符为"_",分割次数为1.
        System.out.println(Arrays.toString(a));//[boo, and_foo]

        String[] b = str.split("_",5);//分割符为"_",最多分割次数为4.
        System.out.println(Arrays.toString(b));//[boo, and, foo]

        String[] c = str.split("_",-2);//分割符为"_",分割尽可能多的次数。
        System.out.println(Arrays.toString(c));//[boo, and, foo]

        String[] d = str.split("o",5);//分割符为"o",最多分割次数为4.
        System.out.println(Arrays.toString(d));//[b, , _and_f, , ]

        String[] e = str.split("o",-2);//分割符为"o",分割尽可能多的次数。
        System.out.println(Arrays.toString(e));//[b, , _and_f, , ]

        String[] f = str.split("o",0);
        System.out.println(Arrays.toString(f));//[b, , _and_f]

        String[] g = str.split("m",0);
        System.out.println(Arrays.toString(g));//[boo_and_foo]
    }
}

[boo, and_foo]
[boo, and, foo]
[boo, and, foo]
[b, , _and_f, , ]
[b, , _and_f, , ]
[b, , _and_f]
[boo_and_foo]

其他

Java编译器在编译期,会自动把所有相同的字符串当作一个对象放入常量池,str1和str3自然是相同的,但是str4与str2原本在不同的常量池,str2==str4不成立。
所以字符串的比较需要使用equals().

方法名方法作用使用方法
contains判断字符串1是否包含str2。str1.contains(str2)
equals比较两个字符串是否相同str1.equals(str2)
length返回字符串的长度String_name.length()
indexof返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。str1.indexOf(“str2”)
startsWith检测字符串是否以指定的前缀开始。str1.startsWith(str2)
regionMatches用于检测两个字符串在一个区域内是否相等。
isEmpty用于判断字符串是否为空。str1.isEmpty()
trim删除字符串的头尾空白符。Str.trim()
toUpperCase字符串小写字符转换为大写。Str.toUpperCase()
toLowerCase()方法将字符串转换为小写。Str.toLowerCase()
substring()返回字符串的子字符串。Str.substring(start)或Str.substring(start, end)
split()方法根据匹配给定的正则表达式(符号如:*.等)来拆分字符串。public String[] split(String regex, int limit)
matches用于检测字符串是否匹配给定的正则表达式。Str.matches(String regex)
charAt通过下表索引str.charAt(int index)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值