学习目标:
掌握API的相关知识学习内容:
1、 String类 2、 正则表达式 3、 StringBuffer和StringBuilder 4、 Math类 4、 Random类学习时间:
2021年5月16日学习产出:
1、 技术笔记 1 遍 2、CSDN 技术博客 1 篇String类
String构造方法
public String()
public String(String str)
public String(byte[] bytes)
public String(char[] value)
public String(byte[] bytes)
String 字符串转化为字节码
字节码再转化成字符串
String s = "abc";
byte[] b = s.getBytes(); //编码
System.out.println(Arrays.toString(b));
String s1 = new String(b);//解码
System.out.println(s1);
String s = "中文";
byte [] b = s.getBytes();//编码
System.out.println(Arrays.toString(b));
String s1 = new String(b);//解码
System.out.println(s1);
String s = "中文";
byte [] b = s.getBytes("GBK");//编码 utf-8
System.out.println(Arrays.toString(b));
String s1 = new String(b,"GBK");//解码 乱码问题:编码和解码两边使用的编码格式不统一
System.out.println(s1);
String s2 = new String(b,2,2,"GBK");//从第二个开始取两个 输出:文
System.out.println(s2);
public String(char[] value)
String 字符串转化为char数组
char 数组转化回字符串
/*
public String(char[] value)
char[] toCharArray()
static String valueOf(char[] chs)
*/
String s = "cab";
char[] chars = s.toCharArray();
/*Arrays.sort(chars);//排序 对String进行排序
System.out.println(Arrays.toString(chars));*/
String s1 = new String(chars);
System.out.println(s1);
String s2 = String.valueOf(chars);
System.out.println(s2);
替换功能
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
String s = " abcdef cg ";
String s1 = s.replace("cd", "CC");
System.out.println(s1);
System.out.println(s.length());
System.out.println(s.trim());//去除字符串前后的空格
split(String regex)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
String s0 = "ab5cd2ef3g";
String[] ss = s0.split("\\d");
System.out.println(Arrays.toString(ss));
String s0 = "abc33d2e44cdg";
String s00 = s0.replaceAll("\\d", "CC");
System.out.println(s00);
String s2 = s0.replaceFirst("cd", "CC");
System.out.println(s2);
正则表达式
正则表达式 :是一种字符串模式匹配语言
指定规则例如:手机号规则 \\d
凡是程序中有输入的地方 必须对输入内容做验证
验证phone这个字符串是手机号? phone.length()==11 phone.charAt(0)==1
基础语法
String s = "a";
boolean res = s.matches("a"); //用字符串对象中matches("正则表达式");
System.out.println(res);
规则
/*
. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
X|Y X 或 Y
*/
实例
String s = "13566268888";
//boolean res = s.matches("\\d?");//一次或一次也没有
//boolean res = s.matches("\\d*");//0次或一次也没有
//boolean res = s.matches("\\d+");//一次或多次
//boolean res = s.matches("\\d{11}");//恰好 n 次
//boolean res = s.matches("\\d{6,10}");//至少 n 次,但是不超过 m 次
//手机号格式规则
boolean res = s.matches("1[356789]\\d{9}");
System.out.println(res);
//qq 数字 6--12 第一位没有0
boolean qq = "012345".matches("[1-9]\\d{5,11}");
System.out.println(qq);
//boolean res = "fshdfT^&&%&".matches("\\D+");
//boolean res = "3 ".matches("\\d\\s+");
//boolean res = "daa".matches("[a-z]+");
//boolean res = "ASAS".matches("[A-Z]+");
//boolean res = "ASsdeaAS".matches("[A-z]+");
//boolean res = "ASsde232aAS".matches("[A-z,0-9]+");
//boolean res = "ASsde232_a_AS".matches("\\w+");
//System.out.println(res);
//通用邮箱格式 \\. 对.符号进行转义
boolean res = "dfgd3AA73@163qq.com.com".matches("\\w{6,10}@\\w{2,5}\\.(com|com\\.cn)");
System.out.println(res);
StringBuffer和StringBuilder
StringBuffer线程安全的,值可变的字符串(指的是底层char[]的值在变)
public StringBuffer() {
super(16);
}
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
StringBuilder线程不安全的,值可变的字符串
//StringBuffer s1 = new StringBuffer(30);//StringBuffer的长度为30
//StringBuffer s = new StringBuffer();//StringBuffer的长度默认为16
StringBuffer s2 = new StringBuffer("abcd");
s2.append("11111");
s2.append("22222");
s2.append("33333");
s2.append("44444");//abcd11111222223333344444
//s2.insert(1, "xxxx");//在第一个位置插入4个x axxxxbcd11111222223333344444
//s2.deleteCharAt(2);//删除第二个位置的字符 abd11111222223333344444
//s2.delete(0,5);//删除指定区间的字符串 包含开始,不包含结尾
//System.out.println(s2.reverse());//翻转s2 44444333332222211111dcba
//System.out.println(s2.replace(0, 5, "ccccc"));//替换指定区间的字符串 包含开始,不包含结尾
String ss = new String(s2);//new String(StringBuffer s)
StringBuilder s1 = new StringBuilder();
String、StringBuffer和StringBuilder的对比
/*
String:值不能变 底层 final char [] value; 一旦需要改变值,必须重新创建一个String value=value+111;
StringBuffer 值可变 char [] value 继承AbstractStringBuilder
StringBuilder 值可变 char [] value 继承 AbstractStringBuilder
StringBuffer 添加synchronized 同步锁 多线程安全
public synchronized StringBuffer append(String str) {
toStringCache = null;
super.append(str);调用父类
return this;
}
StringBuilder 没有添加同步锁 多线程不安全
public StringBuilder append(String str) {
super.append(str); 调用父类
return this;
}
*/
String、StringBuffer和StringBuilder适用场景
String:是字符常量,适用于少量的字符串操作的情况
StringBuilder:适用于单线程下在字符缓冲区进行大量操作的情况
StringBuffer:适用多线程下在字符缓冲区进行大量操作的情况
Math类
abs 绝对值
sqrt 平方根
pow(double a, double b) a的b次幂
max(double a, double b)
min(double a, double b)
random() 返回 0.0 到 1.0 的随机数
long round(double a) double型的数据a转换为long型(四舍五入)
System.out.println(Math.abs(-5));
System.out.println(Math.sqrt(9));
System.out.println(Math.pow(2,3));
System.out.println(Math.random());//大于等于0.0 小于1.0
System.out.println(Math.floor(9.99));//返回 double 类型 向下取整。
System.out.println(Math.ceil(9.001));//返回 double 类型 向上取整
System.out.println(Math.round(9.55));//返回int 类型 向上取整
System.out.println(Math.max(3,5));
Random类
Random类概述
此类用于产生随机数
构造方法
public Random()
成员方法
public int nextInt()
public int nextInt(int n)
Random r = new Random();
//System.out.println(r.nextBoolean());//true
System.out.println(r.nextInt());//1496535772
System.out.println(r.nextInt(33)+1);// 0-32之间随机获取一个值
System.out.println(r.nextLong());//2864970177844707065
byte[] b = new byte[5];//[0,0,0,0,0]
r.nextBytes(b);
System.out.println(Arrays.toString(b));
自定义工具类
/**
* 自定义的String工具类
*/
public class StringUtil {
/**
* 截取文件扩展名
* @param fileName
* @return
*/
public static String subFileType(String fileName){
if (fileName!=null){
return fileName.substring(fileName.lastIndexOf(".")+1);
}
return null;
}
public static void main(String[] args) {
System.out.println(StringUtil.subFileType("hello.js"));
}
}