有梦青年初学JAVA(六)---常用API之String类

String类

1.String类概述

概述

java.lang.String 类代表字符串。Java程序中所有的字符串⽂字(例如 “abc” )都可以被看作是实现此类的实例。

类 String 中包括⽤于检查各个字符串的⽅法,⽐如⽤于⽐较字符串,搜索字符串,提取⼦字符串以及创建具有翻译为⼤写或⼩写的所有字符的字符串的副本。

特点

  1. 字符串不变:字符串的值在创建后不能被更改。
String s1 = "abc";
s1 += "d";
System.out.println(s1); // "abcd"
// 内存中有"abc","abcd"两个对象,
//s1从指向"abc",改变指向,指向了"abcd"。
  1. 因为String对象是不可变的,所以它们可以被共享。
String s1 = "abc";
String s2 = "abc";
// 内存中只有⼀个"abc"对象被创建,同时被s1和s2共享。
  1. “abc” 等效于 char[] data = {‘a’ , ‘b’ , ‘c’} 。
//例如:
String str = "abc";
//相当于:
char data[] = {'a', 'b', 'c'};
String str = new String(data);
// String底层是靠字符数组实现的。

2.使用步骤

查看类
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)

3.常用方法

3.1判断功能的方法

  1. public boolean equals(Object anObject) :将此字符串与指定对象进⾏⽐较。
  2. public boolean equalsIgnoreCase(String anotherString) :将此字符串与指定对象进⾏⽐较,忽略⼤⼩写。

方法演示代码如下:

public class String_Demo01 {
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
System.out.println("-----------");
}
}

Object 是“对象”的意思,也是⼀种引⽤类型。作为参数类型,表示任意对象都可以传递到⽅法中。

3.2获取功能的方法

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 String_Demo02 {
public static void main(String[] args) {
// 创建字符串对象
String s = "helloworld";
// int length():获取字符串的⻓度,其实也就是字符个数
System.out.println(s.length());
System.out.println("--------");
// String concat (String str):将将指定的字符串连接到该字符串的末尾
s = "helloworld";
String s2 = s.concat("**hello bonnie");
System.out.println(s2); // helloworld**hello bonnie
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("--------");
// int indexOf(String str):获取str在字符串对象中第⼀次出现的索引,没有
返回-1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("--------");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("--------");
// String substring(int start, int end):从start到end截取字符串。含
start,不含end。
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}

3.3转换功能的方法

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("-----------");
// 替换字⺟liu为⼤写Liu
String str = "bonnie liu";
String replace = str.replace("liu", "Liu");
System.out.println(replace); // bonnie Liu
System.out.println("-----------");
}
}

CharSequence 是⼀个接⼝,也是⼀种引⽤类型。作为参数类型,可以把String对象传递到⽅法中。

3.4分割功能的方法

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
}
}
}

4.String类的练习

4.1 拼接字符串

定义⼀个⽅法,把数组 {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;
}
}

4.2 统计字符个数

键盘录⼊⼀个字符,统计字符串中⼤⼩写字⺟及数字字符个数。

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 + "个");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值