1. String类概述
概述
java.lang.String
类代表字符串。Java程序中所有的字符串文字(例如"你好Java")都可以被看作是实现此类的实例。
类String中包括用于检查各个字符串的方法,比如用于比较字符串,搜索字符串,提取字符串以及创建具有翻译大写或小写的所有字符串的字符串副本。
特点
- 字符串不变:字符串的值在创建后不能被更改。
- 因为String对象是不可变的,所以它们可以被共享。
- "abc" 等效于 char[] data = {'a','b','c'}。
演示代码如下:
package cn.scy.string;
/**
* @ClassName: Demo01String.class
* @Description: String类
* @Author: 喆
* @Date: 2022/6/2 22:59
* @Software: IntelliJ IDEA
**/
public class Demo01String {
public static void main(String[] args) {
// 1.字符串不变:字符串的值在创建后不能更改
/*
内存种有“abc”,“abcd”两个对象,从str指向“abc”
改变了指向,指向了“abcd"
*/
String str = "abc";
str += "d";
System.out.println(str);
// 2.因为String对象是不可变的,所以它们可以被共享
/* 内存中只有一个"abc"对象被创建,同时被s1和s2共享 */
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
// 3."abc"等效于char[] data = {'a','b','c'}
/* String底层是靠字符数组实现了 */
String s = "abc";
// 相当于
char data[] = {'a','b','c'};
String s3 = new String(data);
System.out.println(s3);
}
}
2. 使用步骤
- 查看类
java.lang.String
:此类不需要导入。
- 查看构造方法
public Stirng()
:初始化新创建String对象,以使其表示空字符序列。
public String(char[] value)
:通过当前参数中的字符数组来构造新的String。
public String(byte[] bytes)
:通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。
代码举例:
package cn.scy.string;
/**
* @ClassName: Demo02String.class
* @Description: 使用步骤
* @Author: 喆
* @Date: 2022/6/2 23:12
* @Software: IntelliJ IDEA
**/
public class Demo02String {
/*
public String():初始化新创建的String对象,以使其表示空字符序列
public String(char[] value):通过当前参数中的祖父数组来构造新的String
public String(byte[] bytes):通过是使用平台的默认字符集解码当前参数中的字节数组来构造新的
*/
public static void main(String[] args) {
// 无参构造
String str = new String();
// 通过字符数组构造
char chars[] = {'a','b','c','d'};
String str2 = new String(chars);
// 通过字节数组构造
byte bytes[] = {97,98,99};
String str3 = new String(bytes);
}
}
3. 常用方法
判断功能的方法
-
public boolean equals (Object anObject):将此字符串与指定对象进行比较。
-
public boolean equalsIgnoreCase(String anotherString):将字符串与指定对象进行比较,忽略大小写。
代码演示:
package cn.scy.string;
/**
* @ClassName: Demo03String.class
* @Description: 判断功能的方法
* @Author: 喆
* @Date: 2022/6/2 23:24
* @Software: IntelliJ IDEA
**/
public class Demo03String {
/*
public boolean equals(Object anObject):将此字符串与指定对象进行比较
public boolean equalsIgnoreCase(String anotherString):将此字符串与指定对象进行比较,忽略大小
*/
public static void main(String[] args) {
// 创建字符串对象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
// boolean equals(Object obj):比较字符串的内容是否相同
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
System.out.println("============");
// boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // true
}
}
Object 是” 对象”的意思,也是一种引用类型。作为参数类型,表示任意对象都可以传递到方法中。
获取功能的方法
-
public int length()
:返回此字符串的长度。 -
public String concat(String str)
:将指定的字符串连接到该字符串的末尾 -
public char charAt(int index)
:返回指定索引处的char值。 -
public int indexOf(String str)
:返回指定子字符串第一次出现在该字符串内的索引 -
public String substring(int beginIndex)
:返回一个子字符串,从beginIndex
开始截取字符串到字符串的结尾 -
public String substring(int beginIndex, int endIndex)
:返回一个字符串,从beginIndex
到endIndex
截取字符串。含beginIndex
,不含endIndex
;
代码演示:
package cn.scy.string;
/**
* @ClassName: Demo04String.class
* @Description: 获取功能的方法
* @Author: 喆
* @Date: 2022/6/2 23:34
* @Software: IntelliJ IDEA
**/
public class Demo04String {
/*
public int length():返回此字符串的长度
public String concat(String str):将指定的字符串连接到该字符串的末尾
public char charAt(int index):返回指定索引处的char值。
public int indexOf(String str):返回指定字符串第一次出现在该字符串内的索引
public String substring(int beginIndex, int endIndex):返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex.
*/
public static void main(String[] args) {
// 创建字符串对象
String s = "helloworld";
// int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length()); // 10
System.out.println("=========");
// String concat(String str):将指定的字符串连接到该字符串的末尾
String s1 = "helloworld";
String s2 = s1.concat("**hello scccy");
System.out.println(s2);// helloworld**hello scy
// char charAt(int index):获取指定索引处的字符
System.out.println(s2.charAt(0)); // h
System.out.println(s2.charAt(1)); // e
System.out.println(s2.charAt(9)); // d
System.out.println("=========");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回值-1
System.out.println(s2.indexOf("l")); // 2
System.out.println(s2.indexOf("i")); // -1
System.out.println(s2.indexOf("c")); // 19
System.out.println("=============");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s2.substring(0,2)); // he
System.out.println(s2.substring(2,6)); // llow
System.out.println("===============");
// String substring(int start,int end):从start到end截取字符串。含start,不含end
System.out.println(s2.substring(2,s2.length())); // lloworld**hello scccy
System.out.println(s2.substring(3,8)); // lowor
}
}
转换功能的方法
- public char[] toCharArray () :将此字符串转换为新的字符数组。
- public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
- public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。
演示代码如下:
package cn.scy.string;
import java.nio.charset.StandardCharsets;
/**
* @ClassName: Demo05String.class
* @Description: 转换功能的方法
* @Author: 喆
* @Date: 2022/6/3 11:45
* @Software: IntelliJ IDEA
**/
public class Demo05String {
/*
public char[] toCharArray():将此字符串转换为新的字符数组
public byte[] getBytes():使用平台的默认字符将该String编码转换为新的字节数组
public String replace(CharSequence target, CharSequence replacement):将与target匹配的字符串使
用replacement字符串替换
*/
public static void main(String[] args) {
// 创建字符串对象
String s = "abc";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.println(chs[i]);
} // a b c
System.out.println("===============");
// byte[] getBytes():把字符串转换为字节数组
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}// 97 98 99
System.out.println("===============");
// 替换字母scy为大写SCY
String str = "scy";
String replace = str.replace("scy","SCY");
System.out.println(replace); // SCY
}
}
CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把String对象传递到方法中。
分割功能的方法
- public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
演示代码如下:
package cn.scy.string;
/**
* @ClassName: Demo06String.class
* @Description: 分割功能的方法
* @Author: 喆
* @Date: 2022/6/3 12:00
* @Software: IntelliJ IDEA
**/
public class Demo06String {
/*
public String[] split(String regex):将字符串按照给定的regex (规则)拆分为字符串数组
*/
public static void main(String[] args) {
// 创建字符串对象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"]
for (int i = 0; i < strArray.length; i++) {
System.out.print(strArray[i]); // aa bb cc
}
}
}
4. String类练习
拼接字符串
定义一个方法,把数组{1,2,3}按照指定个格式拼接成一个字符串。
package cn.scy.string;
/**
* @ClassName: Demo07String.class
* @Description: 拼接字符串
* @Author: 喆
* @Date: 2022/6/4 0:05
* @Software: IntelliJ IDEA
**/
public class Demo07String {
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 i = 0; i < arr.length; i++) {
if(i == arr.length - 1){
s = s.concat(arr[i] + "]");
}else {
s = s.concat(arr[i] + "#");
}
}
return s;
}
}
统计字符个数
键盘录入一个字符,统计字符串中大小写字母及数字字符个数
package cn.scy.string;
import java.util.Scanner;
/**
* @ClassName: Demo08String.class
* @Description: 统计字符个数
* @Author: 喆
* @Date: 2022/6/4 0:13
* @Software: IntelliJ IDEA
**/
public class Demo08String {
public static void main(String[] args) {
// 键盘录入一个字符串数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个字符串数据:");
String s = scanner.nextLine();
// 定义三个统计变量,初始化值都是0
int bigCount = 0;
int smallCount = 0;
int numberCount = 0;
// 遍历字符串,得到每一个字符
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
// 拿字符进行判断
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 +"个");
}
}