String类
文章目录
前言
使用String对象存储字符串
String类位于java.lang包中,具有丰富的方法
计算字符串长度、比较字符串、连接字符串、提取字符串
1、String类构造方法
public class StringDemo01 {
public static void main(String[] args) {
/*
* String str1 = "abc";
* 等效于
* char data[] = {'a','b','c'};
* String str2 = new String(data);*/
String str1 = "abc";
System.out.println("str1 : " + str1); // str1:abc
char data[] = {'a','b','c'};
String str2 = new String(data);
System.out.println("str2 : " + str2); // str2:abc
// 创建String类对象
String s1 = new String();
System.out.println("s1 : " + s1);
byte[] bytes = {97,98,99,100,101,102,103};
String s2 = new String(bytes);
System.out.println("s2 : " + s2); // abcdefg
/*
* public String(byte[] bytes,int offset,int length)
* 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String
* bytes - 要解码为字符的 byte
* offset - 要解码的第一个 byte 的索引
* length - 要解码的 byte 数*/
String s3 = new String(bytes,1,3);
System.out.println("s3 : " + s3); // bcd
System.out.println("------ ------ ------");
/*
* public String(char[] value,int offset,int count)
* 分配一个新的 String,它包含取自字符数组参数一个子数组的字符*/
char[] chars = {'大','湖','名','城'};
String s4 = new String(chars);
System.out.println("s4 : " + s4); // 大湖名城
String s5 = new String(chars,1,2);
System.out.println("s5 : " + s5); // 湖名
System.out.println("------ ------ ------");
/*public String(String original)初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列*/
String s6 = new String("大湖名城,创新高地");
System.out.println("s6 : " + s6); // 大湖名城,创新高地
System.out.println("------ ------ ------");
/*public String(StringBuffer buffer)分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列*/
/*public String(StringBuilder builder)分配一个新的字符串,它包含字符串生成器参数中当前包含的字符序列*/
StringBuilder stringBuilder = new StringBuilder("qwertyuiop");
String s7 = new String(stringBuilder);
System.out.println("s7 : " + s7); // qwertyuiop
// 常用的还是创建String对象,直接传参,public String(String original)
// 或者简写 String s8 = "abcdefghi";
}
}
2、获取字符串长度
public class StringDemo02 {
public static void main(String[] args) {
// 定义一个字符串对象,简写
String s1 = "qwertyuiopasdfghjklzxcvbnm";
// 获取字符串长度
int length = s1.length();
System.out.println("字符串s1的长度:"+length); // 字符串s1的长度:26
/*
* 各种元素长度获取方式:
* 数组长度:数组名.length;
* 集合长度:集合名.size();
* 字符串长度:字符串名.length();
* */
// int[] nums = {11,22,33,44,55,66};
// System.out.println("数组长度为:"+nums.length); // 数组长度为:6
}
}
3、比较字符串内容
public class StringDemo04 {
public static void main(String[] args) {
String s1 = "qwert";
String s2 = "qwert";
String s3 = "asdfg";
String s4 = "ASDFG";
// 比较两个字符串内容是否相同
// public boolean equals(Object anObject)将此字符串与指定的对象比较
// equals()方法比较内容时区分大小写
boolean result1 = s1.equals(s2);
System.out.println("字符串s1和s2内容一致:" + result1); // true
boolean result2 = s1.equals(s3);
System.out.println("字符串s1和s3内容一致:" + result2); // false
boolean result3 = s3.equals(s4);
System.out.println("字符串s1和s2内容一致:" + result3); // false // 即区分大小写
// 比较字符串内容不区分大小写
// public boolean equalsIgnoreCase(String anotherString)将此 String 与另一个 String 比较,不考虑大小写
boolean result4 = s3.equalsIgnoreCase(s4);
System.out.println("不区分大小写比较s3和s4内容一致:" + result4); // true
}
}
4、字符串大小转换
public class StringDemo06 {
public static void main(String[] args) {
String s1 = "qwertQWERT";
System.out.println("s1 = " + s1);
// 将字符串s1中内容全部转换为小写 使用toLowerCase()方法
String lowerCase1 = s1.toLowerCase();
System.out.println("lowerCase1 = " + lowerCase1); // lowerCase1 = qwertqwert
System.out.println("s1 = " + s1); // s1 = qwertQWERT
// 将字符串s1中内容全部转换为大写 使用toUpperCase()方法
String upperCase1 = s1.toUpperCase();
System.out.println("upperCase1 = " + upperCase1); // upperCase1 = QWERTQWERT
System.out.println("s1 = " + s1); // s1 = qwertQWERT
}
}
5、字符串连接方法
1)使用“+”
2)使用String类的concat()方法
public class StringDemo07 {
public static void main(String[] args) {
String s1 = "qwert";
String s2 = "yuiop";
String result = s1 + s2;
System.out.println("字符串s1和s2拼接:" + result); // 字符串s1和s2拼接:qwertyuiop
System.out.println(s1 + s2);
// 使用concat()方法连接字符串
String s3 = s1.concat(s2);
System.out.println("s3 = " + s3); // s3 = qwertyuiop
System.out.println("s1 = " + s1); // s1 = qwert
System.out.println("s2 = " + s2); // s2 = yuiop
System.out.println("------ ------ ------");
// 回顾内容
System.out.println(10 + 20 +"abc"); // 30abc
System.out.println(10 + "abc" + 20); // 10abc20
System.out.println("abc" + 10 + 20); // abc1020
// abc在前与10相加变成字符串,再加20还是字符串
System.out.println("abc" + (10+20)); // abc30
}
}
6、字符串提取方法
public class StringDemo08 {
public static void main(String[] args) {
// 字符串中常用提取方法
// public char charAt(int index)返回指定索引处的 char 值。索引范围为从 0 到 length() - 1
String s1 = "qwert";
char ch1 = s1.charAt(1);
System.out.println("ch1 = " + ch1); // w
System.out.println("------ ------ ------");
/*
* int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引
*
int indexOf(int ch, int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
*
int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引
*
int indexOf(String str, int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
*/
String s2 = "qwertqwertsdfghjk";
// w的ASCII为119
int index1 = s2.indexOf(119);
System.out.println("你要查找的w字符第一次出现的位置:" + index1); // 你要查找的w字符第一次出现的位置:1
int index2 = s2.indexOf('w');
System.out.println("你要查找的w字符第一次出现的位置:" + index2); // 你要查找的w字符第一次出现的位置:1
int index3 = s2.indexOf('a');
System.out.println("你要查找的a字符第一次出现的位置:" + index3); // 你要查找的a字符第一次出现的位置:-1
// 没有查找到则返回-1
System.out.println("------ ------ ------");
int index4 = s2.indexOf(119,4); //
System.out.println("从指定位置开始查找w字符,第一次出现的位置:" + index4);
// 从指定位置开始查找w字符,第一次出现的位置:6
int index5 = s2.indexOf('w',4); //
System.out.println("从指定位置开始查找w字符,第一次出现的位置:" + index5);
// 从指定位置开始查找w字符,第一次出现的位置:6
System.out.println("------ ------ ------");
// 查找字符串
int index6 = s2.indexOf("qwe"); // 查找的是子字符串在字符串中第一次出现的索引
System.out.println("你要查找的字符串在s2中第一次出现的位置:" + index6); // 你要查找的字符串在s2中第一次出现的位置:0
int index7 = s2.indexOf("qwe",4); //
System.out.println("从指定位置开始查找字符串在s2中第一次出现的位置:" + index7); // 从指定位置开始查找字符串在s2中第一次出现的位置:5
int index8 = s2.indexOf("qweqdasd",3);
System.out.println("从指定位置开始查找字符串在s2中第一次出现的位置:" + index8); // 结果为-1
// 从指定的索引开始,如果未出现该字符,则返回-1
System.out.println("------ ------ ------");
String s3 = "abcqwertqweeyuiopqwe";
/*
* int lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引
*
int lastIndexOf(int ch, int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索
*
int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引
*
int lastIndexOf(String str, int fromIndex)
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索
*/
System.out.println("s3的长度:" + s3.length()); // s3的长度:20
int index9 = s3.lastIndexOf(119);
System.out.println("你要查找的字符在s3中最后一次出现的位置:" + index9);
// 你要查找的字符在s3中最后一次出现的位置:18
int index10 = s3.lastIndexOf('w');
System.out.println("你要查找的字符在s3中最后一次出现的位置:" + index10);
// 你要查找的字符在s3中最后一次出现的位置:18
int index11 = s3.lastIndexOf('z');
System.out.println("你要查找的字符在s3中最后一次出现的位置:" + index11);
// 你要查找的字符在s3中最后一次出现的位置:-1 未出现
System.out.println("------ ------ ------");
int index12 = s3.lastIndexOf('w',15);
System.out.println("从指定索引开始,你要查找的字符在s3中最后一次出现的位置:" + index12);
// 从指定索引开始,你要查找的字符在s3中最后一次出现的位置:9
int index13 = s3.lastIndexOf("qwe");
System.out.println("你要查找的子字符串在s3中最后一次出现的位置:" + index13);
// 你要查找的子字符串在s3中最后一次出现的位置:17
int index14 = s3.lastIndexOf("qwe",15);
System.out.println("从指定位置开始,你要查找的子字符串在s3中最后一次出现的位置:" + index14);
// 从指定位置开始,你要查找的子字符串在s3中最后一次出现的位置:8
}
}
7、去除字符串首尾空格
public class StringDemo09 {
public static void main(String[] args) {
String s1 = "qwertyuiop";
/*
* String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串
* */
String substring1 = s1.substring(3);
// substring1 注意string是小写
System.out.println("substring1:" + substring1); // substring1:rtyuiop
System.out.println("s1:" + s1);
String substring2 = s1.substring(3,6);
System.out.println("substring2:" + substring2); // substring2:rty
System.out.println("s1:" + s1); // 原字符串保持不变
System.out.println("------ ------ ------");
/*
* String trim()
返回字符串的副本,忽略前导空白和尾部空白 */
String s3 = " qwert yuiop ";
String trim = s3.trim();
System.out.println("trim : " + trim); // trim : qwert yuiop
// 去除首尾空白
System.out.println("s3:" + s3);
System.out.println("------ ------ ------");
}
}
8、字符串拆分方法
import java.util.Arrays;
public class StringDemo11 {
public static void main(String[] args) {
/*
* String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串
String[] split(String regex, int limit)
根据匹配给定的正则表达式来拆分此字符串
* */
// String[] split(String regex):根据给定正则表达式的匹配拆分此字符串
String s1 = "长亭外 古道边 芳草碧连天 晚风拂 柳笛声残 夕阳山外山";
String[] strings1 = s1.split(" "); // 遍历数组
System.out.println(Arrays.toString(strings1));
String s2 = "长亭外-古道边-芳草碧连天-晚风拂-柳笛声残-夕阳山外山";
String[] strings2 = s2.split("-");
System.out.println(Arrays.toString(strings2));
System.out.println("------ ------ ------");
// byte[] getBytes():使用平台的默认字符将String编码为byte序列 并将结果存储到一个新的byte数组中
String s3 = "abcdefg";
System.out.println("s3:" + s3);
byte[] bytes = s3.getBytes();
System.out.println(Arrays.toString(bytes)); // [97, 98, 99, 100, 101, 102, 103]
System.out.println("s3:" + s3);
// 遍历bytes数组,输出字符内容
for (int i = 0; i < bytes.length; i++) {
System.out.print((char)bytes[i] + " "); // a b c d e f g
}
}
}
9、判断字符串开头结尾
public class StringDemo12 {
public static void main(String[] args) {
//
// public boolean endsWith(String suffix)测试此字符串是否以指定的后缀结束
/*
* boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。
boolean startsWith(String prefix, int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始
* */
String s1 = "HelloWorld.java";
String s2 = "HelloWorld.class";
String s3 = "WorldHell0World.java";
boolean result1 = s1.endsWith(".java");
System.out.println("s1字符串是以.java结尾的:" + result1); // true
boolean result2 = s2.endsWith(".java");
System.out.println("s2字符串是以.java结尾的:" + result2); // false
boolean result3 = s3.startsWith("Hello");
System.out.println("s3是以Hello开头的:" + result3); // false
boolean result4 = s3.startsWith("World");
System.out.println("s3是以world开头的:" + result4); // true
//
boolean result5 = s3.startsWith("world",5);
System.out.println("s3从下标5的地方是以World开头的:" + result5); // false
boolean result6 = s3.startsWith("World",10);
System.out.println("s3从下标10的地方是以world开头:" + result6); // true
}
}
10、获取字符数组中的字符串
public class StringDemo14 {
public static void main(String[] args) {
/*
* static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。
static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的 String
*/
char[] chars = {'大','湖','名','城','创','新','高','地','!'};
String s2 = String.copyValueOf(chars);
System.out.println("s2:" + s2); // s2:大湖名城创新高地!
String s3 = String.copyValueOf(chars,5,4);
System.out.println("s3:" + s3); // s3:新高地!
}
}