Java基础 第二节 第七课
概述
java.lang.String
类代表字符串. Java 程序中所有的字符串文字 ( 例如: “abc” ) 都可以被看作是现实此类的实例.
类 String 中包括用于检查各个字符串的方法, 比如用于比较字符串, 搜索字符串, 提取子字符串以及创建具有翻译的大写或小写的所有字符的字符串的副本.
特点
字符串不变: 字符串的值在创建后不能被更改.
String s1 = "abc";
s1 += "d";
System.out.println(s1); // "abcd"
// 内存中有"abc", "abcd" 两个对象, s1 从指向" abc", 改变指向, 指向了 "abcd"
- 1
- 2
- 3
- 4
因为 String 对象是不可变的, 所以它们可以被共享
String s1 = "abc";
String s2 = "abc";
// 内存中只有一个 "abc" 对象被创建, 同时被 s1 和 s2 共享
- 1
- 2
- 3
“abc” 等效于char[] data={ 'a' , 'b' , 'c' }
.
例如:
String str = "abc";
相当于:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的
- 1
- 2
- 3
- 4
- 5
- 6
- 7
使用步骤
查看类:
java.lang.String
: 此类不需要导入
查看构造方法:
public String()
: 初始化新创建的 String 对象, 以使其表示空字符串序列public String(char[] value)
: 通过当前参数中的字符数组来构造新的 Stringpublic 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);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
常用方法
判断功能的方法
public boolean equals (Object anObject)
: 将此字符串与指定对象进行比较
public boolean equalsIgnoreCase (String anotherString)
: 将此字符串与指定对象进行比较, 忽略大小写.
方法演示, 代码如下:
public class Test65 {
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
// boolean equalsIgnoreCase(String str):
// 比较字符串的内容是否相同, 忽略大小写
System.out.println(s1.equalsIgnoreCase(s2)); // true
System.out.println(s1.equalsIgnoreCase(s3)); // ture
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
注: 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.
方法演示, 代码如下:
public class Test66 {
public static void main(String[] args) {
// 创建字符串对象
String s1 = "helloworld";
// int length(): 获取字符串的长度, 其实也就是字符个数
System.out.println(s1.length()); // 输出结果: 10
// String concat(String str): 将指定的字符串连接到该字符串的末尾
String s2 = "helloworld";
String s3 = s2.concat("**iamlittlewhite");
System.out.println(s3); // 输出结果: helloworld**iamlittlewhite
// char charAt(int index): 获取指定索引处的字符
System.out.println(s1.charAt(0)); // 输出结果: h
System.out.println(s1.charAt(1)); // 输出结果: e
// String indexOf(String str): 获取str在字符串对象中第一次出现的索引, 没有返回-1
System.out.println(s1.indexOf("1")); // 输出结果: -1
System.out.println(s1.indexOf("owo")); // 输出结果: 4
System.out.println(s1.indexOf("ak")); // 输出结果: -1
// String substring(int start): 从start开始截取字符串代字符串结尾
System.out.println(s1.substring(0)); // 输出结果: helloworld
System.out.println(s1.substring(5)); // 输出结果: world
// String substring(int start,int end): 从start到end截取字符串. 含start, 不含end
System.out.println(s1.substring(0, s1.length())); // 输出结果: helloworld
System.out.println(s1.substring(3,8)); // 输出结果: lowor
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
转换功能的方法
public char[] toCharArray ()
: 将此字符串转换为新的字符数组.public byte[] getBytes ()
: 使用平台的默认字符集将该 String 编码转换为新的字节数组.public String replace (CharSequence target, CharSequence replacement)
: 将于 target 匹配的字符串使用 replacement 字符串替换.
方法演示, 代码如下:
public class Test67 {
public static void main(String[] args) {
// 创建字符串对象
String s = "abcde";
// char[] toCharArray(): 把字符串转换为字符数组
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.println(chs[i]);
}
// byte[] getBytes(): 把字符串转换为字节数组
byte[] bytes = s.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
// 替换字母it为大写IT
String str = "iamlittlewhite";
String replace = str.replace("little","big");
System.out.println(replace); // 输出结果: iambigwhite
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
注: CharSequence 是一个接口, 也是一种引用类型. 作为参数类型, 可以把 String 对象传递到方法中.
分割功能的方法
public String[] split(String regex)
: 将此字符串安装给定的 regex (规则) 拆分为字符串数组.
方法演示, 代码如下:
public class Test68 {
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
- 4
- 5
- 6
- 7
- 8
- 9
- 10
String 类的练习
拼接字符串
定义一个方法, 把数组 {1,2,3} 按照指定格式拼接成一个字符串. 格式参照如下: [word1#word2#word3].
public class Test69 {
public static void main(String[] args) {
// 定义一个int类型的数组
int[] arr = {1, 2, 3};
// 调用方法
String s = arrayToString(arr);
// 输出结果
System.out.println("s:" + s);
}
/**
* 写方法实现把数组中的元素按照指定的格式拼接成一个字符串
* @param arr 传入数组
* @return 返回拼接字符串
*/
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
统计字符个数
import java.util.Scanner;
public class Test70 {
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+"个");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
文章来源: iamarookie.blog.csdn.net,作者:我是小白呀,版权归原作者所有,如需转载,请联系作者。
原文链接:iamarookie.blog.csdn.net/article/details/110069537
- 点赞
- 收藏
- 关注作者
评论(0)