package edu.pdsu;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class StringMethodTest{
private static final String s = "你好世界";
@Test
public void testCharAt(){
//获取指定位置的字符
char c = s.charAt(2);
System.out.println(c);//世
}
@Test
public void testLength(){
//获取长度
System.out.println(s.length());//4
}
@Test
public void testIsEmpty(){
//判断字符串是否为空串
System.out.println(s.isEmpty());//false
String s2 = "";
System.out.println(s2.isEmpty());//true
}
@Test
public void testEquals(){
//判断字符串是否相等
String s2 = "你好世界";
System.out.println(s.equals(s2));//true
String s3 = "java";
String s4 = "JAVA";
System.out.println(s3.equals(s4));//false
System.out.println(s3.equalsIgnoreCase(s4));//true
}
@Test
public void testContains(){
//拼接字符串
String s2 = "世界";
String s3 = "world";
System.out.println(s.contains(s2));//true
System.out.println(s.contains(s3));//false
}
@Test
public void testStartsWith(){
//是否已指定字符串开头
String preFix = "你好世";
System.out.println(s.startsWith(preFix));//true
String url = "https://www.bilibili.com";
System.out.println(url.startsWith("https"));
}
@Test
public void testEndWith(){
//是否已指定字符串结尾
String suffix = "好世界";
System.out.println(s.endsWith(suffix));//true
String url = "https://www.bilibili.com";
System.out.println(url.endsWith("cn"));//false
}
@Test
public void testCompareTo(){
//按照字典比较(包括带小写)
String s1 = "A";
String s2 = "a";
System.out.println(s1.compareTo(s2));//-32
}
@Test
public void testCompareToIgnoreCase(){
//按照字典比较(忽略大小写)
String s1 = "A";
String s2 = "a";
System.out.println(s1.compareToIgnoreCase(s2));//0
}
@Test
public void testIndexOf(){
//指定字符串第一次出现的位置
String s1 = "蔡徐坤,陈立农,范丞丞";
System.out.println(s1.indexOf("坤"));//2,哇真的是你啊
}
@Test
public void testLastIndexOf(){
//指定字符串最后出现的位置
String s1 = "蔡徐坤,陈立农,范丞丞";
System.out.println(s1.lastIndexOf("丞"));//10
}
@Test
public void testToCharArray(){
//将字符串转成字符数组
char[] chars = s.toCharArray();
for (char aChar : chars) {
System.out.print(aChar + " ");//你 好 世 界
}
}
@Test
public void testToLowerCase(){
//转成小写
String s1 = "JAVA";
System.out.println(s1.toLowerCase());//java
}
@Test
public void testToUpperCase(){
//转成大写
String s1 = "java";
System.out.println(s1.toUpperCase());//JAVA
}
@Test
public void testSubString(){
//截取字符串(包含开头,不包含结尾)
String s1 = "蔡徐坤,陈立农,范丞丞";
String s2 = s1.substring(2);
String s3 = s1.substring(2,5);
System.out.println(s2);//坤,陈立农,范丞丞
System.out.println(s3);//坤,陈
}
@Test
public void testTrim(){
//去除空格
String s1 = " 蔡徐坤 陈立农 范丞丞 ";
String s2 = "\u3000蔡徐坤 陈立农";
System.out.println(s1.trim());//蔡徐坤 陈立农 范丞丞
System.out.println(s2);// 蔡徐坤 陈立农
System.out.println(s2.trim());// 蔡徐坤 陈立农
}
@Test
public void testStrip(){
//去除所有空格
String s1 = "\u3000蔡徐坤 陈立农 ";
System.out.println(s1.strip());//蔡徐坤 陈立农
System.out.println(s1.stripLeading() + ";");//蔡徐坤 陈立农 ;
System.out.println(s1.stripTrailing() + ";");// 蔡徐坤 陈立农;
}
@Test
public void testIntern(){
//将指定字符串加入常量池并返回
String s1 = "你好";
String s2 = "世界";
String s3 = s1 + s2;
String s4 = s3.intern();
String s5 = "你好世界";
System.out.println(s4 == s5);//true
}
@Test
public void testJoin(){
//填充指定内容
String s1 = String.join(",", "java", "C++", "mysql");
System.out.println(s1);//java,C++,mysql
//创建一个集合对象
List<String> list = new ArrayList<>();
list.add("java");
list.add("c++");
list.add("mysql");
String s2 = String.join(",",list);//java,C++,mysql
}
@Test
public void testValueOf(){
//转String
String s1 = String.valueOf(1000);//Nmber类型
System.out.println(s1);//"1000"
String s2 = String.valueOf(true);//布尔类型
System.out.println(s2);//"true"
String s3 = String.valueOf(new Object());//引用类型
System.out.println(s1);//"java.lang.Object@5f8ed237"是个地址,相当于直接toString了
}
@Test
public void testReplace(){
//替换指定内容
String s1 = "蔡徐坤,陈立农,范丞丞";
String s2 = s1.replace("蔡徐坤","鸡哥");
System.out.println(s2);//鸡哥,陈立农,范丞丞
}
@Test
public void testReplaceAll(){
//按照正则表达替换
String s1 = "a1v2v5v4fd5d3f4";
String s2 = s1.replaceAll("\\d","鸡");
System.out.println(s2);//a鸡v鸡v鸡v鸡fd鸡d鸡f鸡
}
@Test
public void testSplit(){
//按照正则表达进行字符串的拆分,返回数组
String s1 = "蔡徐坤,陈立农,范丞丞";
String[] split = s1.split(",");
for (String string : split) {
System.out.println(string);
}
//蔡徐坤
//陈立农
//范丞丞
}
@Test
public void testMatches(){
//根据正则表达匹配字符串是否符合
String emailRegExp = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
String email1 = "1145141919@yarimasunei.com";
String email2 = "114514.com";
System.out.println(email1.matches(emailRegExp));//true
System.out.println(email2.matches(emailRegExp));//false
}
}
Java中String的常用方法
于 2024-04-03 19:37:12 首次发布