特点
1.字符串不变:字符串的值在创建后不能被更改。
String s1 = "abc";
s1 += "d";
System.out.println(s1); // "abcd"
// 内存中有"abc","abcd"两个对象,s1从指向"abc",改变指向,指向了"abcd"。
2.因为String对象是不可变的,所以它们可以被共享。
String s1 = "abc";
String s2 = "abc";
// 内存中只有一个"abc"对象被创建,同时被s1和s2共享。
3."abc"
等效于 char[] data={ 'a' , 'b' , 'c' }
。
例如:
String str = "abc";
相当于:
char data[] = {
'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的。
查看类
java.lang.String
:此类不需要导入。
查看构造方法
public String()
:初始化新创建的 String对象,以使其表示空字符序列。public String(char[] value)
:通过当前参数中的字符数组来构造新的String。public String(byte[] bytes)
:通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。- 构造举例,代码如下:
// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {
'a', 'b', 'c'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = {
97, 98, 99 };
String str3 = new String(bytes);```
不同构造方法的字符串比较
package com.web;
public class Test42 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
byte[] b4 = {
97,98,99};
String s4 = new String(b4);//s4 = abc
// String底层是靠字符数组实现的。
char[] c5 ={
'a','b','c'};
String s5 = new String(c5);//s5 = abc
System.out.println(s1==s2);//true 没有开辟新的内存空间,abc被s1,s2共享
System.out.println(s1==s3);//false 开辟了新的内存空间
System.out.println(s1==s4);//false 开辟了新的内存空间
System.out.println(s1==s5);//false 开辟了新的内存空间
System.out.println(s1.equals(s2));//true 只比较值大小
System.out.println(s1.equals(s3));//true 只比较值大小
System.out.println(s1.equals(s4));//true 只比较值大小
System.out.println(s1.equals(s5));//true 只比较值大小
}
}
字符串常用方法
package com.web;
public class Test41 {
public static void main(String[] args) {
System.out.println("abc".equalsIgnoreCase("ABC"));//true
//创建字符串对象
String s = "helloworld";
// int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length());//10
System.out.println("--------");
// String concat (String str):将将指定的字符串连接到该字符串的末尾.
String s0 = "helloworld";
String s2 = s0.concat("**hello itheima");
System.out.println(s2);// helloworld**hello itheima
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));//h
System.out.println(s.charAt(1));//e
System.out.println("--------");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回-1
System.out.println(s.indexOf("l"));//2
System.out.println(s.indexOf("owo"));//4
System.out.println(s.indexOf("ak"));//-1
System.out.println("--------");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s.substring(0));//helloworld
System.out.println(s.substring(5));//world
System.out.println("--------");
// String substring(int start,int end):从start到end截取字符串。含start,不含end。
System.out.println(s.substring(0, s.length()));//helloworld
System.out.println(s.substring(3,8));//lower
}
}
转换功能的方法
public char[] toCharArray ()
:将此字符串转换为新的字符数组。public byte[] getBytes ()
:使用平台的默认字符集将该 String编码转换为新的字节数组。public String replace (CharSequence target, CharSequence replacement)
:将与target匹配的字符串使用replacement字符串替换。
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "abcde";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("-----------");
// byte[] getBytes ():把字符串转换为字节数组
byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
System.out.println("-----------");
// 替换字母it为大写IT
String str = "itcast itheima";
String replace = str.replace("it", "IT");
System.out.println(replace); // ITcast ITheima
System.out.println("-----------");
}
}
CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。
分割功能的方法
public String[] split(String regex)
:将此字符串按照给定的regex(规则)拆分为字符串数组。
方法演示,代码如下:
public class String_Demo03 {
public static void main(String[] args) {
//创建字符串对象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"]
for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
}
拼接字符串
定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。格式参照如下:[word1#word2#word3]。
public class StringTest1 {
public static void main(String[] args) {
//定义一个int类型的数组
int[] arr = {
1, 2, 3};
//调用方法
String s = arrayToString(arr);
//输出结果
System.out.println("s:" + s);
}
/*
* 写方法实现把数组中的元素按照指定的格式拼接成一个字符串
* 两个明确:
* 返回值类型:String
* 参数列表:int[] arr
*/
public static String arrayToString(int[] arr) {
// 创建字符串s
String s = new String("[");
// 遍历数组,并拼接字符串
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
s = s.concat(arr[x] + "]");
} else {
s = s.concat(arr[x] + "#");
}
}
return s;
}
}
统计字符个数
键盘录入一个字符,统计字符串中大小写字母及数字字符个数
public class StringTest2 {
public static void main(String[] args) {
//键盘录入一个字符串数据
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串数据:");
String s = sc.nextLine();
//定义三个统计变量,初始化值都是0
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
//遍历字符串,得到每一个字符
for(int x=0; x<s.length(); x++) {
char ch = s.charAt(x);
//拿字符进行判断
if(ch>='A'&&ch<='Z') {
bigCount++;
}else if(ch>='a'&&ch<='z') {
smallCount++;
}else if(ch>='0'&&ch<='9') {
numberCount++;
}else {
System.out.println("该字符"+ch+"非法");
}
}
//输出结果
System.out.println("大写字符:"+bigCount+"个");
System.out.println("小写字符:"+smallCount+"个");
System.out.println("数字字符:"+numberCount+"个");
}
}