1 String类
1.1 String类的概述
String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
String str = "HelloWorld";
str = "HY";
字符串是常量;它们的值在创建之后不能更改,这是什么意思呢?其实就是说一旦这个字符串确定了,那么就会在内存区域中就生成了这个字符串。字符串本身不能改变,但str变量中记录的地址值是可以改变的。
public class TestDemo{
public static void main(String[] args){
String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //true
//因为String重写了equals方法,
//建立了字符串自己的判断相同的依据(通过字符串对象中的字符来判断)
}
}
运行结果

1.2 String类构造方法
构造方法是用来完成String对象的创建,下图中给出了一部分构造方法

TestDemo.java
public class TestDemo{
public static void main(String[] args){
//创建String对象,字符串中没有内容
String s1 = new String();
s1 = "abc";
//创建String对象,把数组元素作为字符串的内容
byte[] bys = new byte[]{97,98,99,100};
String s2 = new String(bys);
//把一部分数组元素作为字符串的内容,参数offset为数组元素的起始索引位置,
//参数length为要几个元素
String s3 = new String(bys,1,2);
//字符串的本质就是一个字符类型的数组
char[] chs = new char[]{'a','b','c','d','e'};
//创建String对象,把数组元素作为字符串的内容
String s4 = new String(chs);
String s5 = new String(chs,0,4);
//创建String对象,字符串内容为abc
String s6 = new String("abc");
System.out.println("s1="+s1);
System.out.println("s2="+s2);
System.out.println("s3="+s3);
System.out.println("s4="+s4);
System.out.println("s5="+s5);
System.out.println("s6="+s6);
}
}
运行结果

1.3 String类的方法
1.字符串中有多少个字符

String s1 = "abcdef";
int len = s1.length();
System.out.println("len="+len);
2.获取部分字符串

TestDemo.java
public class TestDemo{
public static void main(String[] args){
String str = "abcde";
//返回一个新字符串,内容为指定位置开始到字符串末尾的所有字符
String s1 = str.substring(1);
//返回一个新字符串,内容为指定位置开始到指定位置结束所有字符
String s2 = str.substring(2,4);
System.out.println("str="+str);
System.out.println("s1="+s1);
System.out.println("s2="+s2);
}
}
运行结果

3.字符串是否以指定字符串开头(结尾同理)

TestDemo.java
public class TestDemo{
public static void main(String[] args){
String str = "StringDemo.java";
//判断是否以给定字符串开头
boolean b1 = str.startsWith("Demo");
boolean b2 = str.startsWith("String");
//判断是否以给定字符串结尾
boolean b3 = str.endsWith("java");
System.out.println("b1="+b1);
System.out.println("b2="+b2);
System.out.println("b3="+b3);
}
}
运行结果

4.字符串中是否包含另一个字符串

TestDemo.java
public class TestDemo{
public static void main(String[] args){
String str = "abcde";
//判断是否包含指定字符串,包含则返回第一次出现该字符串的索引,不包含则返回-1
int index = str.indexOf("bcd");
//判断是否包含指定字符串,包含返回true,不包含返回false
boolean b2 = str.contains("bcd");
System.out.println("index="+index);
System.out.println("b2="+b2);
}
}
运行结果

5.将字符串转成一个字符数组(或者字节数组)

TestDemo.java
public class TestDemo{
public static void main(String[] args){
String str = "abcde";
char[] chs = str.toCharArray();
byte[] bytes = str.getBytes();
System.out.println("打印chs");
for(int i = 0;i < chs.length;i++){
System.out.print(chs[i]+" ");
}
System.out.println();
System.out.println("打印bytes");
for(int i = 0;i < bytes.length;i++){
System.out.print(bytes[i]+" ");
}
System.out.println();
}
}
运行结果

6.判断两个字符串中的内容是否相同

public class TestDemo{
public static void main(String[] args){
String str = "abcde";
String str2 = "abcde";
String str3 = "hello";
String str4 = "ABCDE";
boolean b1 = str.equals(str2);
boolean b2 = str.equals(str3);
boolean b3 = str.equalsIgnoreCase(str4);
System.out.println("b1="+b1);
System.out.println("b2="+b2);
System.out.println("b3="+b3);
}
}
运行结果

自己动手在API中查找,并进行方法使用。
判断该字符串的内容是否为空的字符串
获取给定的字符,在该字符串中第一次出现的位置
获取该字符串中指定位置上的字符
把该字符串转换成 小写字符串
把该字符串转换成 大写字符串
在该字符串中,将给定的旧字符,用新字符替换
在该字符串中, 将给定的旧字符串,用新字符串替换
去除字符串两端空格,中间的不会去除,返回一个新字符串
1.4 String类方法使用练习
题目一:获取指定字符串中,大写字母、小写字母、数字的个数。
思路:
1.为了统计大写字母、小写字母、数字的个数。创建3个计数的变量。
2.为了获取到字符串中的每个字符,进行字符串的遍历,得到每个字符。
3.对得到的字符进行判断,如果该字符为大写字母,则大写字母个数+1;如果该字符为小写字母,则小写字母个数+1;如果该字符为数字,则数字个数+1。
4.显示大写字母、小写字母、数字的个数
代码:TestDemo.java
public class TestDemo{
public static void main(String[] args){
method("ASDGHababc992939");
}
public static void method(String str){
int bigCount = 0; //大写字母个数
int smallCount = 0; //小写字母个数
int numberCount = 0; //数字的个数
for(int i = 0;i < str.length();i++){
//获取指定位置上的字符
char ch = str.charAt(i);
if(ch >= 'A' && ch <= 'Z'){
bigCount++;
}else if(ch >= 'a' && ch <= 'z'){
smallCount++;
}else if(ch >= '0' && ch <= '9'){
numberCount++;
}
}
System.out.println("大写字母个数:"+bigCount);
System.out.println("小写字母个数:"+smallCount);
System.out.println("数字个数:"+numberCount);
}
}
运行结果

题目二:将字符串中,第一个字母转换成大写,其他字母转换成小写,并打印改变后的字符串。
思路:
1.把字符串分为两个部分,第一部分为字符串中第一个字母,第二部分为剩下的字符串。
2.把第一部分字符串转换成大写字母,把第二部分字符串转换成小写字母
3.把两部分字符串连接在一起,得到一个完整的字符串
代码:
public class TestDemo{
public static void main(String[] args){
String s = convert("ASDGHababc992939");
System.out.println(s);
}
public static String convert(String str){
//获取第一部分字符串
String start = str.substring(0,1);
//获取剩余字符串
String end = str.substring(1);
//把第一部分字符串转换成大写字母
//把第二部分字符串转换成小写字母
String big = start.toUpperCase();
String small = end.toLowerCase();
return big+small;
}
}
运行结果

题目三:查询大字符串中,出现指定小字符串的次数。如“hellojava,nihaojava,javazhenbang”中查询出现“java”的次数。
思路:
1.在大串中,查找小串出现的位置,出现了就次数+1
2.在上次小串出现位置的后面继续查找,需要更改大串的内容为上次未查询到的字符串。
3.回到第一步,继续查找小串出现的位置,直到大串中查询不到小串为止
代码:
public class TestDemo{
public static void main(String[] args){
int i = getCount("abcdsabcdeabcde","ab");
System.out.println(i);
}
public static int getCount(String big,String small){
int count = 0; //出现小字符串的次数
int index = -1; //出现小字符串的位置
/*
while的循环条件三步骤:
步骤一. big.indexOf(small) 获取小串在大串中出现的位置
步骤二. 把小串出现的位置,赋值给变量index
步骤三. 判断出现的位置是否为-1, 如果位置等于-1,
说明大串中已经查询不到小串了;如果位置不等于-1,
那么,进行循环,完成次数累加与修改大串的操作
*/
while((index = big.indexOf(small)) != -1){
count++; //出现加1
big = big.substring(index+1);
}
return count;
}
}
运行结果

2 StringBuffer类
2.1 StringBuffer类介绍
在学习String类时,API中说字符串缓冲区支持可变的字符串,什么是字符串缓冲区呢?接下来我们来研究下字符串缓冲区。
StringBuffer又称为可变字符序列,它是一个类似于 String 的字符串缓冲区,通过某些方法调用可以改变该序列的长度和内容。
原来StringBuffer是个字符串的缓冲区,即就是它是一个容器,容器中可以装很多字符串。并且能够对其中的字符串进行各种操作。
2.2 StringBuffer的方法使用

代码演示:TestDemo.java
public class TestDemo{
public static void main(String[] args){
//创建一个字符串缓冲区对象。用于存储数据
StringBuffer sb = new StringBuffer();
//添加字符串
sb.append("Hello World");
//在指定位置插入
sb.insert(2,"it");
////删除
sb.delete(1,4);
//替换指定范围的内容
sb.replace(0,1,"case");
String s = sb.toString();
System.out.println(s);
//截取字符串
String s1 = sb.substring(5);
System.out.println(s1);
//删除指定位置上的字符
sb.delete(10,11);
String s2 = sb.toString();
System.out.println(s2);
}
}
运行结果

注意:append、delete、insert、replace、reverse方法调用后,返回值都是当前对象自己,所以说,StringBuffer它可以改变字符序列的长度和内容。
2.3 对象的方法链式调用
在我们开发中,会遇到调用一个方法后,返回一个对象的情况。然后使用返回的对象继续调用方法。这种时候,我们就可以把代码现在一起,如append方法一样,代码如下:
//创建一个字符串缓冲区对象,用于存储数据。
StringBuffer sb = new StringBuffer();
//不断的添加数据后,要对缓冲区的最后的数据进行操作,必须转成字符串才可以。
String str = sb.append(true).append("hehe").toString();
2.4 StringBuffer练习
练习:int[] arr = {34,12,89,68};将一个int[]中元素转成字符串 格式 [34,12,89,68]
public class TestDemo{
public static void main(String[] args){
int[] arr = {34,12,89,68};
String s = method(arr);
System.out.println(s);
}
public static String method(int[] arr){
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int i = 0;i < arr.length;i++){
if(i != arr.length-1){
sb.append(arr[i]+",");
}else{
sb.append(arr[i]+"]");
}
}
return sb.toString();
}
}
运行结果

无论多少数据,数据是什么类型都不重要,只要最终变成字符串就可以使用StringBuffer这个容器
网友评论