29天!
字符串
1.字符串概述和特点、构造方法和直接创建
package Demo1901;
/*
* java.lang.String类代表字符串。
* API当中说,Java程序中的所有字符串字面值(如“abc”)都作为此类的实例实现。
* 其实就是说,程序当中所有的双引号字符串,都是String类的对象。(就算没有new,也照样是)
*
* 字符串的特点:
* 1.字符串的内容永不可变【重点】。
* 2.正是因为字符串不可改变,所以字符串是可以共享的。
* 3.字符串效果上相当于是char[]字符数组。但是底层原理是byte[]字节数组。
*
* 创建字符串的常见3+1种方式。
* 三种构造方法:
* public String();创建一个空白字符串,不含任何内容。
* public String(char[] array);根据字符数组的内容,来创建对应的字符串。
* public String(byte[] array);根据字节数组的内容,来创建对应的字符串。
* 一种直接创建:
* String str = "hello";//右边直接写双引号
*
* 注意:直接写上双引号,就是字符串对象。
* */
public class Demo01String {
public static void main(String[] args) {
//首先使用空参构造
String str1 = new String();
System.out.println("第一个字符串为:" + str1);
//根据字符数组创建字符串
char[] charArray = {'A', 'B', 'C'};
String str2 = new String(charArray);
System.out.println("第二个字符串为:" + str2);
//根据字节数组创建字符串
byte[] byteArray = {97, 98, 99};
String str3 = new String(byteArray);
System.out.println("第三个字符串为:" + str3);//第三个字符串为:abc
//直接创建
String str4 = "hello";
System.out.println("第四个字符串为:" + str4);
}
}
2.字符串的常量池
package Demo1901;
//双引号直接写的在常量池当中,new的不在池当中
/*
* 字符串常量池:程序当中直接写上的双引号字符串,就在字符串常量池中。
*
* 对于基本类型来说,==就是数值的比较。
* 对于引用类型来说,==是进行【地址值】的比较。
* */
public class Demo02StringPool {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
char[] charArray = {'a', 'b', 'c'};
String str3 = new String(charArray);
System.out.println(str1 == str2);//true
System.out.println(str1 == str3);//false
System.out.println(str2 == str3);//false
}
}
3.字符串的比较相关方法
package Demo1901;
/*
* ==是进行对象的地址值比较,如果确实需要字符串的内容比较,可以使用两个方法,
*
* public boolean equals(Object obj):参数可以是任何对象,
* 只有参数是一个字符串且内容相同的才会给true,否则返回false。
* 注意事项:
* 1.任何对象都能用Object进行接收。
* 2.equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样。
* 3.如果比较双方一个常量一个变量,推荐把常量字符写在前面。
* 推荐:“abc”.equals(str)
*
* public boolean equalsIgnoreCase(String str);忽略大小写进行比较
* */
public class Demo03StringEquals {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
char[] charArray = {'H','e','l','l','o'};
String str3 = new String(charArray);
//boolean b1 = str1.equals(str2);
//boolean b2 = str1.equals(str3);
//boolean b3 = str3.equals("Hello");
System.out.println(str1.equals(str2));//true
System.out.println(str1.equals(str3));//true
System.out.println(str3.equals("Hello"));//true
System.out.println("Hello".equals(str2));//true
String str5 = null;
System.out.println("abc".equals(str5));//推荐:false
//System.out.println(str5.equals("abc"));//不推荐,报错,空指针异常NullPointerException
String str6 = "HAHa";
String str7 = "haha";
System.out.println(str6.equalsIgnoreCase(str7));//true
}
}
4.字符串的获取相关方法
package Demo1901;
/*
* String当中与获取相关的常用方法有:
* Public int length():获取当前字符串中含有的字符个数,拿到字符长度。
* public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串
* public char charAt(int index):获取指定索引位置的单个字符。(索引从0开始)
* public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有,返回-1值。
* */
public class Demo04StringGet {
public static void main(String[] args) {
//1.获取字符串的长度
int length = "shbhdsgvhsdbnc".length();
System.out.println("字符串的长度为:" + length);//字符串的长度为:14
//2.拼接字符串
String str1 = "hello";
String str2 = "Hello";
String concat = str1.concat(str2);//全新的第三个字符串,不影响str1和str2,因为字符串是常量
System.out.println(str1);//hello
System.out.println(str2);//Hello
System.out.println("拼接字符串为:" + concat);//拼接字符串为:helloHello
//3.获取指定索引位置的单个字符
char c = str1.charAt(1);
System.out.println("指定索引位置的单个字符为" + c);//指定索引位置的单个字符为e
//4.查找参数字符串在本字符串当中首次出现的索引位置
String str3 = "helloHelloHello";
String str4 = "Hello";
int i = str3.indexOf(str4);
int i1 = str4.indexOf(str3);
System.out.println("首次出现的位置为:" + i);//5,索引仍然从0开始
System.out.println("首次出现的位置为:" + i1);//-1
}
}
5.字符串的截取
package Demo1901;
/*
* 字符串的截取方法:
* public String substring(int index):截取从参数位置一直到字符串末尾,返回新字符串。
* public String substring(int begin,int end):截取从begin开始,一直到end结束,中间的字符串。
* 备注:[begin,end):包含左边,不包含右边。类似于random
* */
public class Demo05SubString {
public static void main(String[] args) {
String str1 = "HelloWorld";
String substring1 = str1.substring(5);
System.out.println(str1);//HelloWorld
System.out.println(substring1);//World
String substring2 = str1.substring(4, 7);
System.out.println(substring2);//owo
//下面这种写法,字符串的内容仍然是没有改变的
//下面有两个字符串:“Hello”,“java”
//strA当中保存的是地址值
//本来地址值是hello的0x666;
//后来地址值变成了Java的0x999;
String strA = "hello";
System.out.println(strA);//hello
strA = "java";
System.out.println(strA);//java
}
}
6.字符串的转换相关方法
package Demo1901;
/*
* String当中与转换相关的常用方法有:
* public char[] toCharArray():将当前字符串拆分为字符数组作为返回值。
* public byte[] getBytes():获得当前字符串底层的字节数组。
* public String replace(CharSequence oldString,CharSequence newString):
* 将所有出现的老字符串替换为新的字符串,返回替换之后的结果新字符串。
* CharSequence的意思就是可以接收字符串
* */
public class Demo06StringConvert {
public static void main(String[] args) {
//1.转换为字符数组
char[] chars = "Hello".toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);//Hello
}
System.out.println("======");
//2.转换为字节数组
byte[] bytes = "abc".getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.print(bytes[i]);
}
//3.替换
String str1 = "helloWorld";
String replace1 = str1.replace("World", "java");
System.out.println(str1);//helloWorld
System.out.println(replace1);//hellojava
String replace2 = str1.replace("l", "*");
System.out.println(str1);//helloWorld
System.out.println(replace2);//he**oWor*d
//4.替换在生活中的应用,游戏中的敏感词汇替换
String str2 = "会不会玩啊,你大爷的";
String replace3 = str2.replace("你大爷的", "****");
System.out.println(replace3);//会不会玩啊,****
}
}
7.字符串的分割方法
package Demo1901;
/*
* 分割字符串的方法:
* public String[] split(String regex):按照参数的规则,将字符串切分成若干部分。
*
* 注意事项:
* spilt方法的参数其实是一个“正则表达式”,以后学习。
* 但是要注意,如果按照英文句点“.”进行切分,必须写“\\.”(两个反斜杠)
* */
public class Demo07StringSplit {
public static void main(String[] args) {
String str1 = "aaa,bbb,ccc";
String[] split = str1.split(",");
for (int i = 0; i < split.length; i++) {
System.out.println("分割结果为:" + split[i]);//分割结果为:aaa 分割结果为:bbb 分割结果为:ccc
}
//System.out.println("分割结果为:" + split);//分割结果为:[Ljava.lang.String;@312b1dae
System.out.println("=====");
String str2 = "aaa bbb ccc";
String[] split1 = str2.split(" ");
for (int i = 0; i < split1.length; i++) {
System.out.println(split1[i]);
}
String str3 = "aaa.bbb.ccc";
//String[] split2 = str3.split(".");//无法输出结果,说明循环未执行//0
String[] split2 = str3.split("\\.");
System.out.println(split2.length);
for (int i = 0; i < split2.length; i++) {
System.out.println(split2[i]);
}
}
}
8.按指定格式拼接字符串
package Demo1901;
/*
* 题目:
* 定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。
* 格式参照如下:[word1#word2#word3]
*
* 思路:
* 1.首先准备一个int数组
* 2.方法convertString,返回值为字符串,参数为数组
* 3.格式[word1#word2#word3]。用到for循环、字符串拼接,每个数组元素之前都有一个word字样,分隔符使用的是#
*
* 注意事项:
* 在方法中,每次拼接的是上次的结果,而不是原字符串
* */
public class Demo08StringPractice01 {
public static void main(String[] args) {
int[] array = {1, 2, 3};
System.out.println(convertString(array));
}
/*public static String convertString(int[] array){
String str = "";
//String result = "";//将后面的str换为result会导致只输出[word3]
System.out.print("[");
for (int i = 0; i < array.length; i++) {
if (i == (array.length-1)){
str = str.concat("word" + array[i] +"]");
}else {
str = str.concat("word" + array[i] + "#");
}
}
return str ;
}*/
//更简单的思路,直接将字符串初始化为左括号,后面直接用+=连接
public static String convertString(int[] array) {
String str = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length-1){
str += "word" + array[i] + "]";
}else {
str += "word" + array[i] + "#";
}
}
return str;
}
}
9.练习
9.1题目1:
定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。格式参照如下:[word1#word2#word3]
package Demo1901;
/*
* 题目:
* 定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串。
* 格式参照如下:[word1#word2#word3]
*
* 思路:
* 1.首先准备一个int数组
* 2.方法convertString,返回值为字符串,参数为数组
* 3.格式[word1#word2#word3]。用到for循环、字符串拼接,每个数组元素之前都有一个word字样,分隔符使用的是#
*
* 注意事项:
* 在方法中,每次拼接的是上次的结果,而不是原字符串
* */
public class Demo08StringPractice01 {
public static void main(String[] args) {
int[] array = {1, 2, 3};
System.out.println(convertString(array));
}
/*public static String convertString(int[] array){
String str = "";
//String result = "";//将后面的str换为result会导致只输出[word3]
System.out.print("[");
for (int i = 0; i < array.length; i++) {
if (i == (array.length-1)){
str = str.concat("word" + array[i] +"]");
}else {
str = str.concat("word" + array[i] + "#");
}
}
return str ;
}*/
//更简单的思路,直接将字符串初始化为左括号,后面直接用+=连接
public static String convertString(int[] array) {
String str = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length-1){
str += "word" + array[i] + "]";
}else {
str += "word" + array[i] + "#";
}
}
return str;
}
}
9.2 题目2:
键盘输入一个字符串,并且统计其中各种字符出现的次数。种类有:大写字母、小写字母、数字、其他
package Demo1901;
/*
* 题目:
* 键盘输入一个字符串,并且统计其中各种字符出现的次数。
* 种类有:大写字母、小写字母、数字、其他
*
* 思路:
* 1.键盘输入
* 2.将字符串转化为字符数组,判断数组每个元素的所处位置,符合某项则加一
* 数字:48-57(0-9);大写字母:65-90;小写字母:97-122
* 另外一种思路,可以不查表,直接ch >= '0' && ch <= '9'
* */
import java.util.Scanner;
public class Demo09StringPractice02Count {
public static void main(String[] args) {
//键盘输入获取字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输出字符串:");
String str = sc.next();
//将输入字符串转换为字符数组
char[] chars = str.toCharArray();
//定义各个类型个数
int num = 0, upperCase = 0, lowerCase = 0, other = 0;
//for循环遍历字符数组
for (int i = 0; i < chars.length; i++) {
//第一种思路:查表,很麻烦
/*if (chars[i] >= 48 && chars[i] <= 57) {
num++;
} else if (chars[i] >= 65 && chars[i] <= 90) {
upperCase++;
} else if (chars[i] >= 97 && chars[i] <= 122) {
lowerCase++;
} else {
other++;
}*/
//第二种思路:不查表,容易
if (chars[i] >= '0' && chars[i] <= '9'){
num ++;
} else if (chars[i] >= 'A' && chars[i] <= 'Z'){
upperCase++;
} else if (chars[i] >= 'a' && chars[i] <= 'z'){
lowerCase++;
} else {
other++;
}
}
System.out.println("数字为" + num + "个");
System.out.println("大写字母为" + upperCase + "个");
System.out.println("小写字母为" + lowerCase + "个");
System.out.println("其他字符为" + other + "个");
}
}