Java字符串

一、String类

String类是Java中用于处理字符串的类,属于java.lang包。字符串是一系列字符的序列,而String类是不可变的,也就是说,一旦创建了String对象,其值无法更改。如果需要对字符串进行频繁的修改,通常使用StringBuffer或StringBuilder类。

1.1 声明字符串

在Java语言中,字符串必须包含在一对双引号("")之内。例如:

"23.23"、"ABCDE"、"你好"

以上这些都是字符串常量,字符串常量可以是系统能够显示的任何文字信息,甚至可以是单个字符。

在Java中由双引号("")包围的都是字符串,不能作为其他数据类型来使用,如“1+2”的输出结果不可能是3。

可以通过以下语法格式来声明字符串变量:

String str;

声明的字符串变量必须经过初始化才能使用,否则编译器会报出“变量未被初始化错误”。

1.2 创建字符串

在Java语言中,将字符串作为对象来处理,因此可以像创建其他类对象一样来创建字符串对象。创建对象要使用类的构造方法。String类的常用构造方法如下:

1.String(char a[])

该方法用一个字符数组a创建String对象,代码如下:

char[] a = {'g', 'o', 'o', 'd'};
String s = new String(a);

等价于:

String s = new String("good");

2.String(char a[],int offset,int length)

该方法提取字符数组a中的一部分创建一个字符串对象。参数offset表示开始截取字符串的位置,length表示截取字符串的长度,方法的范围是左闭右开的,即索引为offset的字符可以取到,索引为length的字符取不到。代码如下:

char[] a={'h','e','l','l','o','w','o','r','l','d'};
        String s =new String(a,0,5);
        System.out.println(s);//hello

等价于:

String s = new String("hello");
String s=new String("student");

除通过以上几种使用String类的构造方法来创建字符串变量之外,还可以通过字符串常量的引用赋值给一个字符串变量来创建字符串。代码如下:

String str1, str2;
str1 = "We are students";
str2 = "We sre students";

二、连接字符串

对于已声明的字符串,可以对多个字符串进行连接,也可使字符串与其他数据类型进行连接。

2.1 连接多个字符串

使用“+”运算符可以实现连接多个字符串的功能。“+”运算符可以连接多个String对象并产生一个新的Srting对象。

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String s1=new String("hello");
        String s2=new String("world");
        String s=s1+s2;
        System.out.println(s);//helloworld
    }
}

2.2  连接其他数据类型

字符串也可同其他数据类型进行连接。如果将字符串同其他数据类型数据进行连接,会将其他数据类型的数据直接转换成字符串。

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        int price = 66;
        String book = "Java程序设计";
        System.out.println(book + price + "元");//Java程序设计66元
    }
}

​​​​

三、获取字符串信息

3.1 获取字符串长度

使用String类的length()方法可以获取声明的字符串对象的长度,代码示例如下:

str.length();

获取字符串长度,代码如下:

String str = "HelloWorld";
System.out.println(str.length());//10

3.2 字符串查找

String类提供了两种查找字符串的方法,即indexOf()和lastIndexOf()方法。这两种方法都允许在字符串中搜索指定条件的字符或字符串。indexOf()方法返回的是参数字符串在此字符串中第一次出现处的索引,从指定的索引开始。如果没有检索到,方法返回值为-1,lastIndexOf()方法返回参数字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索,如果没有检索到,方法返回值为-1

1.indexOf(String str, int fromIndex)

返回参数字符串在此字符串中第一次出现处的索引,从指定的索引开始。如果没有检索到,方法返回值为-1

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String str = "HelloWorld";
        System.out.println(str.indexOf("o",0));//4
        System.out.println(str.indexOf("o",-3));//4
        System.out.println(str.indexOf("o",644));//-1
    }
}

在Java语言中,String对象是用数组表示的,字符串下标是0~length()-1。

当fromIndex是负数时,indexOf会将fromIndex视为0,即从字符串的开头开始查找。这是 Java 的一个内置行为,负数的fromIndex会自动调整为0。

当fromIndex的值大于字符串的长度时,indexOf 会直接返回-1,因为从该索引开始已经没有任何字符,无法进行查找。

2.lastIndexOf(String str, int fromIndex)

返回参数字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索,如果没有检索到,方法返回值为-1

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String str = "HelloWorld";
        System.out.println(str.lastIndexOf("o",7));//6
        System.out.println(str.lastIndexOf("o",-3));//-1
        System.out.println(str.lastIndexOf("o",644));//6
        System.out.println(str.lastIndexOf("",57));//10
    }
}

如果lastIndexOf()方法中的参数是空字符串""(注意没有空格),则返回的结果与调用length()方法的返回结果相同。

当fromIndex是负数时,lastIndexOf会直接返回-1,因为从负数索引开始没有任何字符可以查找。因此,查找不会进行,返回 -1

当fromIndex大于字符串的长度时,lastIndexOf 会将fromIndex视为字符串的最后一个字符索引,即它会从字符串的末尾开始查找。

需要注意的是,所谓最后一次出现处的索引,指的是正向的最后一次出现,而非反向。只有从指定的索引处查找是反向的,这里容易混淆

3.3 获取指定索引位置的字符

使用charAt()方法可将指定索引处的字符返回,语法如下:

str.charAt(int index);
package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String str = "helloworld";
        System.out.println("字符串str中索引位置是6的字符为:" + str.charAt(6));//字符串str中索引位置是6的字符为:o
    }
}

四、字符串操作

4.1 获取字符串

通过String类的substring()方法可以对字符串进行截取。

substring(int beginindex, int endindex)

返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始

参数说明:
beginIndex-起始索引-(包含)

endlndex-结束索引-(不包含)

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String s = "hello world! hello everyone";
        System.out.println(s.substring(13));//hello everyone
        System.out.println(s.substring(6,12));//world!
    }
}

 4.2 去除空格

trim()方法返回去除字符串开头和结尾空格的字符串

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String s = "   hello world! hello everyone" ;
        System.out.println(s.trim());//hello world! hello everyone
    }
}

4.3 字符串替换

replace(char oldChar char newChar )

返回一个新的字符串,通过用 newChar 替换此字符串中出现的所有 oldChar 得到的

 
package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String s = "hello world! hello everyone";
        System.out.println(s.replace("hello","hi"));//hi world! hi everyone
    }
}

如果要替换的字符oldChar在字符串中重复出现多次,replace()方法会将所有oldChar全部替换成newChar。

要替换的字符oldChar的大小写要与原字符串中字符的大小写保持一致

五、转换大小写

5.1转换为大写

toUpperCase()方法:使用默认语言环境的规则将此 String 中的所有字符都转换为大写

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String s = "Hello World! Hello Everyone";
        System.out.println(s.toUpperCase());//HELLO WORLD! HELLO EVERYONE
    }
}

5.2转换为小写

toLowerCase()方法:使用默认语言环境的规则将此 String 中的所有字符都转换为小写

package oneHundredDayPlan.threeDay;

public class Two {
    public static void main(String[] args) {
        String s = "Hello World! Hello Everyone";
        System.out.println(s.toLowerCase());//hello world! hello everyone
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值