目录
1、String类的概述
什么是字符串
字符串是由一个或多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
String类的概述
通过JDK提供的API,查看String类的说明
a:字符串字面值"abc"也可以看成是一个字符串对象。
b:字符串是常量,一旦被创建,就不能被改变。值不能被改变,你能改变的是引用或者说指向。
2、String类的构造方法
常见构造方法
public String():空构造
public String(String original):把字符串常量值转成字符串
package org.westos.demo2;
public class MyTest {
public static void main(String[] args) {
// String() 初始化一个新创建的 String 对象,使其表示一个空字符序列。
String s = new String();//""
s="aaa";
System.out.println(s.toString());//String类重写了toString()方法,会输出字符串的内容
/* String(String original)
初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;
换句话说,新创建的字符串是该参数字符串的副本。*/
String s1 = new String("abc");
System.out.println(s1);
}
}
public String(byte[] bytes):把字节数组转成字符串
public class MyTest1 {
public static void main(String[] args) {
//public String( byte[] bytes):把字节数组转成字符串
byte[] bytes = {97,98,99,100};
String s = new String(bytes);
System.out.println(s);//输出结果:abcd
}
}
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
(index:表示的是从第几个索引开始, length表示的是长度)
public class MyTest1 {
public static void main(String[] args) {
//public String( byte[] bytes):把字节数组转成字符串
//从0索引处转换字节数组中3个字节
byte[] bytes = {97,98,99,100};
String s = new String(bytes,0,3);
System.out.println(s);//输出结果:abc
}
}
public String(char[] value):把字符数组转成字符串
public class MyTest2 {
public static void main(String[] args) {
char[] chars={'你','好','啊','好','好','学','习'};
System.out.println(""+'你');//输出结果:你
String s=""+chars[0]+chars[1]+chars[2];
System.out.println(s);//输出结果:你好啊
String s1 = new String();
for (int i = 0; i < chars.length; i++) {
s1+=chars[i];
}
System.out.println(s1);//输出结果:你好啊好好学习
}
}
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
public class MyTest2 {
public static void main(String[] args) {
char[] chars={'你','好','啊','好','好','学','习'};
String s = new String(chars);
System.out.println(s);//输出结果:你好啊好好学习
//转换字符数组的一部分,从3索引开始转换4个字符
String s1 = new String(chars,3,4);
System.out.println(s1);//输出结果:好好学习
}
}
public int length():返回此字符串的长度
public class MyTest2 {
public static void main(String[] args) {
char[] chars={'你','好','啊','好','好','学','习'};
String s = new String(chars);
System.out.println(s);//输出结果:你好啊好好学习
int length = s.length();
System.out.println(length);//7
}
}
3、String的特点一旦被创建就不能改变
String的特点:
一旦被创建就不能改变 因为字符串的值是在堆内存的常量池中划分空间 分配地址值的
public class MyTest2 {
public static void main(String[] args) {
// 字符串是常量;它们的值在创建之后不能更改。
// 改变的是他的指向
String s = "hello";
s = "world" + "java";
System.out.println(s);
}
}
String s = new String("hello") 和String s = "hello";的区别
public class MyTest3 {
public static void main(String[] args) {
// String s = new String("hello") 和String s = "hello";的区别
String s = "hello"; //构建一个Hello对象
//构建两个对象
String s1 = new String("abc");
}
}
对比:
public class MyTest5 {
public static void main(String[] args) {
String s="hello";
String s2="world";
String s3 = "hello";
String s4 = new String("hello");
System.out.println(s==s2); //false
System.out.println(s2==s3); //false
System.out.println(s4==s);//false
System.out.println(s4==s3);//false
System.out.println(s==s3);//true
}
}
4、String类的常见面试题
看程序写结果
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4);
System.out.println(s3.equals(s4));String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);
System.out.println(s5.equals(s6));
public class MyTest6 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2); //false
System.out.println(s1.equals(s2)); //true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 == s4); //false
//字符串类,重写了父类的equals()方法,比较的是,字符串字面上的内容是否相同。
System.out.println(s3.equals(s4)); //true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6); //true
System.out.println(s5.equals(s6)); //true
}
}
问题2:
public class MyTest2 {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Hel" + "lo"; // 编译器会优化,编译期可以确定
String s4 = "Hel" + new String("lo");
String s5 = new String("Hello");
String s6 = s5.intern();
String s7 = "H";
String s8 = "ello";
String s9 = s7 + s8;
System.out.println(s1 == s2); //true
System.out.println(s1 == s3); //true
System.out.println(s1 == s4); //false
System.out.println(s1 == s9); //false
System.out.println(s4 == s5); //false
System.out.println(s1 == s6); //true
}
}
5、String类的判断功能
String类的判断功能
public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(): 判断字符串的内容是否为空串""
public class MyTest3 {
public static void main(String[] args) {
// String类的判断功能
// public boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
System.out.println("abc".equals("Abc"));//false
// public boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println("abc".equalsIgnoreCase("ABc"));//true
// public boolean contains(String str):判断字符串中是否包含传递进来的字符串
System.out.println("我爱Java".contains("爱Ja"));//true
// public boolean startsWith(String str):判断字符串是否以传递进来的字符串开头
System.out.println("我爱Java".startsWith("我爱"));//true
// public boolean endsWith(String str):判断字符串是否以传递进来的字符串结尾
System.out.println("我爱Java".endsWith("a"));//true
// public boolean isEmpty():判断字符串的内容是否为空串""
System.out.println("".isEmpty());//true
}
}
6、String类的获取功能
String类的获取功能
public int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符
public class MyTest4 {
public static void main(String[] args) {
String str = "abcdef";
int length = str.length();
System.out.println(length);//输出结果:6
String str1 = "我好困我要睡觉";
char c = str1.charAt(str1.length() - 1);
System.out.println(c);//输出结果:觉
}
}
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch,int fromIndex): 返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
lastIndexOf:返回最后一次出现的索引
public class MyTest4 {
public static void main(String[] args) {
//获取某个字符在原字符中第一次出现的索引位置,如果没检索到,返回 -1
String str = "我好困睡觉我要睡觉我好困睡觉我要睡觉哈哈哈";
int a = str.indexOf("好");
System.out.println(a);
int b = str.indexOf("睡觉");
System.out.println(b);
//从指定索引处开始查找
int c = str.indexOf("睡觉", 5);
System.out.println(c);
int d = str.indexOf("睡觉", str.indexOf("睡觉")+1);
System.out.println(d);
//lastIndexOf从后往前查找
int lastIndexOf = str.lastIndexOf("睡觉",13);
System.out.println(lastIndexOf);
}
}
public String substring(int start): 从指定位置开始截取字符串,默认到末尾。含头不含尾
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串
public class MyTest5 {
public static void main(String[] args) {
String str = "我好困睡觉我要睡觉我好困睡觉我要睡觉哈哈哈";
//根据起始索引和终止索引,从原字符串中,截取一部分字符串,返回。 含头不含尾
String s = str.substring(0,5);
System.out.println(s);
//从给定索引截取到末尾
String s1 = str.substring(10);
System.out.println(s1);
}
}
7、字符串的遍历
public class MyTest {
public static void main(String[] args) {
//字符串的遍历
//字符串也是编有索引的,从0开始
//"abcdefafdsfasdfadsf"
//字符串底层,用字符数组来存储的。
String str="abcefefgenh";
//根据索引获取单个字符
char c = str.charAt(0);
System.out.println(c);
System.out.println("==================================================");
for (int i = 0; i < str.length(); i++) {
char c1 = str.charAt(i);
System.out.println(c1);
}
System.out.println("===============================");
for (int j = str.length()-1; j >= 0; j--) {
char c1 = str.charAt(j);
System.out.println(c1);
}
}
}
8、统计不同类型字符个数
统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数
public class MyTest3 {
public static void main(String[] args) {
//需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)
String str = "aaaAAA888";
int da = 0;
int xiao = 0;
int num = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
da++;
} else if (ch >= 'a' && ch <= 'z') {
xiao++;
} else if (ch >= '0' && ch <= '9') {
num++;
}
}
System.out.println("大写字母有"+da+"个");
System.out.println("小写字母有" + xiao + "个");
System.out.println("数字字符有" + num + "个");
}
}
9、String类的转换功能
String的转换功能:
public byte[] getBytes(): 把字符串转换为字节数组。
public class Mytest {
public static void main(String[] args) {
//把字符串转换成字节数组
String str = "asdfghjkl";
byte[] bytes = str.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
String s = new String(bytes);
System.out.println(s);
//UTF-8 一般汉字会占 3个字节 GBK 一个汉字占两个字节
str="我";
byte[] bytes1 = str.getBytes();
for (int i = 0; i < bytes1.length; i++) {
System.out.println(bytes1[i]);
}
byte[] bytes2={-26,-120,-111};
String s1 = new String(bytes2);
System.out.println(s1);
}
}
public char[] toCharArray(): 把字符串转换为字符数组。
public class Mytest1 {
public static void main(String[] args) {
//把字符串转换成字符数组
String str = "我好困我要睡觉";
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
//把字符数组转换成字符串
String s = new String(chars);
System.out.println(s);
}
}
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串。
注意:String类的valueOf方法可以把任意类型的数据转成字符串。
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。
public class MyTest3 {
public static void main(String[] args) {
int num=100;
double num2=3.14;
boolean f=false;
String s=num+"";//"100"
String s2=num2+"";//"3.14"
String s3=f+"";//"false"
String s1 = String.valueOf(num);
System.out.println(s1);//"100"
String s4 = String.valueOf(true); //"true"
System.out.println(s4);
String s5 = String.valueOf(3.5); //"3.5"
System.out.println(s5);
String s6 = String.valueOf(new char[]{'a', 'b', 'c'});
String s7 = new String(new char[]{'a', 'b', 'c'});
System.out.println(s6);
System.out.println(s7);
System.out.println("============");
String s9="abc";
String s8 = s9.toUpperCase();
System.out.println(s8);
String s10="EFG";
String s11 = s10.toLowerCase();
System.out.println(s11);
//拼接字符串
String str2="aaa"+"bbb"+"ccc";
String sss = "AAA".concat("BBB");
System.out.println(sss);
String concat = "AAA".concat("BBB").concat("CCC").concat(str2);
System.out.println(concat);
}
}
10、String类的其他功能
String的替换功能
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
public class Mytest3 {
public static void main(String[] args) {
String str = "我好困我要睡觉";
String s1 = str.replace("困", "饿");
System.out.println(s1);
String s2 = str.replace("睡觉", "吃饭");
System.out.println(s2);
String s3 = str.replace("饿", "累").replace("吃饭", "休息");
System.out.println(s3);
}
}
运行结果:
String的去除字符串两空格
public String trim() 去除两端空格
public class MyTest4 {
public static void main(String[] args) {
String str2=" a b c ";
//去除字符串左右两端的空格
String trim = str2.trim();
System.out.println(trim);
}
}
String的按字典顺序比较两个字符串
public int compareTo(String str)
会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
如果连个字符串一摸一样,返回的就是0
public int compareToIgnoreCase(String str)
跟上面一样 只是忽略大小写的比较
public class MyTest3 {
public static void main(String[] args) {
/* public int compareTo (String str)会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
如果两个字符串一摸一样 返回的就是0*/
int i = "Abcdef".compareTo("Abc");
System.out.println(i);
int i1 = "Abcdef".compareTo("Abcdef");
System.out.println(i1);
int i2 = "abc".compareTo("Abcd");//A的ASCll码是65,,a的ASCll码是97
System.out.println(i2);//32
//不区分大小写
i = "abc".compareToIgnoreCase("Abc");
System.out.println(i);
}
}