string类
/*创建字符串的方式:
1.直接赋字面量
2.new String()
new String(有参)
*/
String str1 = "hello";
System.out.printIn(str1);
String str2 = new String("hello");
System.out.printIn(str2);
String str3 = new String();
System.out.printIn(str3);
String str4 = new String(new char[]{'a', 'b', 'c'});
System.out.printIn(str4);
String str5 = new String(new byte[]{97, 98, 99});
System.out.printIn(str5);
System.out.printIn(str5+1);
System.out.printIn('1'+1);
String类的常用方法
//1.length()获取字符串的长度
int length = str.length();
System.out.println(length);
//2.equals(字符串)比较字符串
boolean b = str.equals("helloworld");
System.out.println(b);
//3.indexOf(int ch)
int index = str.indexOf(97);
System.out.println(index);
//4.lastindexOf(int ch)
index = str.lastIndexOf(97);
System.out.println(index);
// indexOf(String ch)
System.out.println(str.indexOf("l"));
// lastindexOf(String ch)
System.out.println(str.lastIndexOf("l"));
//5.charAt(int index)通过索引返回字符
System.out.println(str.charAt(0));
//6.endsWith(字符串)
System.out.println(str.endsWith("world"));
//7.startsWith(字符串)
System.out.println(str.startsWith("hello"));
// 8.equalsIgnoreCase() 比较字符,并忽略大小写
boolean flag = str.equalsIgnoreCase("HelloWorld");
System.out.println(flag);
//9.toUpperCase() 转大写
String s = str.toUpperCase();
System.out.println(s);
//10.toLowerCase() 转小写
String s1 = str.toLowerCase();
System.out.println(s1);
//11.commpareto比较大小
System.out.println(str.compareTo("bworld"));
//12.isEmpty()字符串是否为空
System.out.println(str.isEmpty());
//13.contains(字符串)是否包含指定字符串
System.out.println(str.contains("wo"));
//14.将int类型转为字符串
String s2 = String.valueOf(15);
//15.toCharArray()将字符串转字符数组
char[] chars = str.toCharArray();
System.out.println(Arrays.toString(chars));
//16.concat(字符串)拼接字符串
String abc = str.concat("abc");
System.out.println(abc);
//17.trim()去掉前后空格
String str3 = " helloworld ";
String trim = str3.trim();
System.out.println(trim.length());
//18.replace()旧值,新值):新值替换旧值
System.out.println(str);
String replace = str.replace("hello", "aaa");
System.out.println(replace);
//19.substring(int start, int end)
String str4="abcdef";
System.out.println(str4.substring(1, 5));
System.out.println(str4.substring(0));
//20.split(分隔符)
String str5 = "a-b-c";
String[] split=str5.split("-");
System.out.println(Arrays.toString(split));
StringBuffer类
//创建StringBuffer类
StringBuffer sb = new StringBuffer();
sb.append("abcd").append("hijk");
System.out.println(sb);
sb.insert(1, "pp");
System.out.println(sb);
}
public static void remove(){
StringBuffer sb = new StringBuffer("abcdefj");
sb.delete(1, 5);
System.out.println(sb);
sb.deleteCharAt(1);
System.out.println(sb);
sb.delete(0, sb.length());
System.out.println(sb);
}
public static void update(){
StringBuffer sb = new StringBuffer("abcdefg");
sb.setCharAt(1, 'h');
System.out.println(sb);//abcdefg
sb.replace(1, 5, "hello");
System.out.println(sb);
sb.reverse();
System.out.println(sb);
}
//截取
public static void sub() {
StringBuffer sb =new StringBuffer();
System.out.println(sb.capacity());
sb.append("itcast123");
String substring = sb.substring(1, 5);
System.out.println(substring);
}
StringBuilder类相比于StringBuffer类的线程更不安全,但性能更高。
StringBuilder和StringBuffer类中没有重写equals()函数
RunTime类
/*
* Runtime运行时类
* 被封装在JVM中
* 1.exec(“文件路径名”)执行文件
* 2.availableProcessors()处理器个数
* 3.maxMemory()最大内存容量
* 4.freeMemory()空闲的内存容量
* */
public static void main(String[] args) throws IOException, InterruptedException {
Runtime runtime = Runtime.getRuntime();
int i = runtime.availableProcessors();
System.out.println(i);
long l =runtime.maxMemory();
System.out.println(l/1024/1024+"MB");
System.out.println(runtime.freeMemory()/1024/1024+"MB");
Process pro = runtime.exec("D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQScLauncher.exe");//打开QQ
Thread.sleep(3000);
pro.destroy();
}
Math类
Random类
没有指定种子,以时间戳为种子
随机数应用场景
String[] strArr = {"一等奖", "二等奖", "三等奖", "谢谢惠顾"}
int index= getRandomIndex(strArr);
System.out.printIn(strArr[index]);
/*
思路:
先获取[0, 3)之间的随机下标
通过下标获取奖品
*/
public static int getRandomIndex(String[] strArr){
Random rd = new Random();
int num = rd,nextInt(strArr.length);
return num;
}
BigInteger类
BigInteger 大整数类
-- 处理数字范围超过long类型时使用
构造方法:
public BigInteger(String str){}
常用方法:
add 加
subtract
muilpity
divide
max
min
divideAndRemainder 返回一个数组,分别存储商与余
BigDecimal类(使用进行浮点数计算)
Data类
Calendar类(抽象类)
Instant类
LocalDate类
LocalTime类
LocalDateTime类
Duration和Period类
DateFormat类
SimpleDateFormat类
数字格式化类(NumberFormat)
包装类
/*
* 包装类的创建方式
* 自动封箱:
* 基本数据类型 转换 引用数据类型
* 自动拆箱
* 引用数据类型 转换 基本数据类型
*
* 字符串z数字
* Integer.parseInt(String str)
* */
正则表达式