今天继续学习java的API。
String类
转换功能的方法有:
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
String toLowerCase()
String toUpperCase()
String concat(String str)
Stirng[] split(分割符);
public static void main(String[] args) throws UnsupportedEncodingException {
String s = new String("非凡");
byte [] bytes = s.getBytes();//传输的时候,需要把字符串转为字节数组转码后,默认使用 平台的字符集
System.out.println(Arrays.toString(bytes));
String s1 = new String(bytes); //接收到字节数组后,把字节数组转为字符串,即解码
System.out.println(s1);
String s = new String("非凡");
byte [] bytes = s.getBytes("utf-8");//传输的时候,需要把字符串转为字节数组,即转码
System.out.println(Arrays.toString(bytes));
//String s1 = new String(bytes, "gbk");//接收到字节数组后,把字节数组转为字符串 解码
String s1 = new String(bytes,0,3,"utf-8");
System.out.println(s1);
String s = "sfg";
char[] chars = s.toCharArray(); //字符串转字符数组
Arrays.sort(chars);
String s2 = new String(chars);//字符数组转为字符串
System.out.println(s2);
int i = Integer.parseInt("10");
Integer ii = new Integer("20");
String s = String.valueOf(10);
ii = null;
String s1 = String.valueOf(ii);//建议将其他类型转为String时,使用valueof() 引用的值 如果为null,返回null字符串形式,不报错
//ii = null; 引用值为null 就会报空指针异常
//String s2 = ii.toString();//Integer中的toString()
String toLowerCase()
String toUpperCase()
String concat(String str)
Stirng[] split(分割符);
String s3 = "ABCdefg";
System.out.println(s3.toLowerCase());
System.out.println(s3.toUpperCase());
String s4 = "abcd"+10+true;
String s5 = s4.concat("efg");//连接一个字符串 //s4+=10;
System.out.println(s5);
String s6 = "ab|cde|fg";
String [] sarray = s6.split("\\|");//[ab,cde,fg] 使用指定的分割符号拆分字符串
\\使用“|”符号时需要转码,即“\\|”
System.out.println(Arrays.toString(sarray));
}
替换功能的方法有:
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
去除字符串两空格的方法:String trim()
public static void main(String[] args) {
//替换功能
String replace(char old,char new)
String replace(String old,String new)
replaceAll(String regex, String replacement)
replaceFirst(String regex, String replacement)
// 去除字符串两端空格
String trim()
String s1 = "a6bcde2fcdg1";
System.out.println(s1.replace("c", "C"));
System.out.println(s1.replace("cd", "CC"));//用指定的字符串替换指定目标子串
System.out.println(s1.replaceAll("\\d",""));//以正则表达式方式进行匹配替换,所有匹配的替换
System.out.println(s1.replaceFirst("\\d",""));//以正则表达式方式进行匹配替换,替换第一个
String s2 = " abc d ";
System.out.println(s2.length());
System.out.println(s2.trim().length());//去掉字符串两端的空格
}
StringBuffer类
在我们使用String时,String的值一旦确定就不能改变,如果我们要修改一个String对象里的值需要多次拼接和创建新对象,浪费空间和时间,这时我们就可以采用Stringbuffer类。
StringBuffer类是可变带缓冲区的字符串 ,如果我们需要大量的字符串拼接,建议使用StringBuffer,但我们要修改字符串的内容时,可以使用以下方法。
public static void main(String[] args) {
StringBuffer s = new StringBuffer("abcd");//创建了一个4+16长度的char数组
s.append("efg");//7
s.append("hij");//10
s.append("klm");//13
s.append("nop");//16
s.append("qls");//19
s.append("tuvwx");//22 装不下了,创建一个新的数组,赋给StringBuffer对象中的value[]
s.insert(0, "ABC");//向指定的位置插入元素
s.delete(0,3);//删除指定位置多个元素
s.deleteCharAt(0);//删除指定位置一个元素
s.replace(0,3,"CCC");//替换指定位置多个元素
s.reverse();//逆序输出
String s1 = s.substring(0,5);
System.out.println(s1);
System.out.println(s);
}
}
String类StringBuffer类StringBuilder区别
StringBuffer和StringBuilder区别:
相同点: 底层实现方式是完全一致, 类的内部有一个char数组,没有并final修饰,之后对象字符串进 行增,删操作,都是对底层数组直接操作。
不同点:StringBuffer是多线程操作安全的,方法都被synchronized关键字修饰。
public synchronized int length() {
return count;
}
StringBuilder是多线程不安全的. 单线程情况下建议使用。
String, StringBuffer和StringBuilder区别:
String 值是不可改变的,少量的拼接操作可以使用,但是大量的字符串拼接不建议使用。
StringBuffer和StringBuilder都是值可变的。
正则表达式
正则表达式,又称规则表达式,(Regular Expression,在代码中常简写为regex、regexp或RE),是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符"),是计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列匹配某个句法规则的字符串,通常被用来检索、替换那些符合某个模式(规则)的文本。
下面是正则表达式的符号使用体现代码:
import java.lang.reflect.Array;
import java.util.Arrays;
public class Demo6 {
public static void main(String[] args) {
String n="1334567890";
String m="zxcvbadgglapkgf";
String name="Tom";
String email="2278383401@qq.com";
//boolean a=n.matches("\\d");//匹配一个数字
//boolean a=n.matches("\\d*");//匹配0个或多个数字
//boolean a=n.matches("\\d+");//匹配一个或多个数字
//boolean a=n.matches("[0-9]+");//匹配一次或多次0-9
//boolean a=n.matches("[0-9]");//匹配一次0-9
//boolean a=n.matches("[1][3]\\d[0-9]+");//匹配一个电话号码
//boolean a=n.matches("\\D");//匹配一个非数字字符
//boolean a=m.matches("[a-z]+");//匹配一个或多个小写字母
//boolean a=m.matches("[A-Z]+");//匹配一个或多个大写字母
//boolean a=name.matches("[A-Z][a-z]+");//匹配一个英文名字
//boolean a=n.matches("[1][3][0-9]+");//匹配一个电话号码
//boolean a=n.matches("\\D");//匹配一个非数字字符
//boolean a=m.matches("[a-z]+");//匹配一个或多个小写字母
boolean a=email.matches("\\w+@\\w{2,5}\\.(com|com\\.cn)");//匹配一个邮箱
System.out.println(a);
String b="1234a123a12";
String []c=b.split("a");
System.out.println(Arrays.toString(c));
}
}
更多的代码体现可以关注以下网页:
https://baike.baidu.com/item/%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/1700215?fr=aladdin
Math类
在java中,java.lang.Math为我们提供了一系列静态方法用于科学计算。
这些方法的参数和返回值类型一般为double型。
其中我们常用的方法有:
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型(四舍五入)
public class Demo7 {
public static void main(String[] args) {
System.out.println(Math.max(12,34));
System.out.println(Math.min(14.3,24.5));
System.out.println(Math.abs(-5));
System.out.println(Math.ceil(2));
System.out.println(Math.sqrt(9));
}
}
Random类
Random类,可以产生随机数让我们使用。
Random类的构造方法是:public Random(){}
Random类的成员方法有很多,下面代码体现了部分成员方法:
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Random;
public class Demo8 {
public static void main(String[] args) {
Random random=new Random();
int a=random.nextInt();
System.out.println(a);
System.out.println(random.nextBoolean());
System.out.println(random.nextFloat());
System.out.println(random.nextInt(28));//随机生成一个0(包括)-28(不包括)之间的数
byte []b =new byte[10];
random.nextBytes(b);
System.out.println(Arrays.toString(b));
}
}
Date类
Date类可以使用系统时间,返回系统当前的时间。
import java.util.Date;
public class Demo9 {
public static void main(String[] args) {
/*
Date 类
用来表示时间,所表示的时间截取的是程序当时运行时的时间
*/
//无参构造方法
Date date=new Date();//创建一个新的date对象
System.out.println(date);//截取当前时间
System.out.println(date.getTime());//1656317028183 返回从1970 ---至今的毫秒差
System.out.println(date.getDate());//截取今天是这个月的第几天
System.out.println(date.getHours());//截取小时
System.out.println(date.getMinutes());//截取分钟
System.out.println(date.getDay());//截取现在是这周的第几天
System.out.println(date.getSeconds());//截取秒
System.out.println(date.getYear()+1900);//截取年份
//有参构造方法
Date date1=new Date(1656316932390L);//传入一个long类型
}
}
Calendar类
Calendar类 日历类
比date类有更丰富的方法
这是也是一个返回时间的类,是一个抽象类
在实现时,使用特点的子类对象,使用geiInstance方法创建即可
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Demo0 {
/*
Calendar类 日历类
比date类有更丰富的方法
这是也是一个返回时间的类,是一个抽象类
在实现时,使用特点的子类对象,使用geiInstance方法创建即可
*/
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
Calendar calendar=new GregorianCalendar();
System.out.println(calendar.DATE);
System.out.println(calendar.DAY_OF_MONTH);
System.out.println(calendar.DAY_OF_WEEK);
System.out.println(calendar.DAY_OF_YEAR);
System.out.println(calendar.HOUR_OF_DAY);
System.out.println(calendar.getTimeInMillis());//1656317028183 返回从1970.1.1 00:00:00 ---至今的毫秒差
}
}
SimpleDateFormat类
SimpleDateFormat类 简单日期格式化,可以将时间转换成其他格式,要注意日期的书写格式。
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo11 {
/*
SimpleDateFormat类 简单日期格式化
可以将时间转换成其他格式
*/
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS E");
String time=simpleDateFormat.format(new Date());
System.out.println(time);
}
}
BigInteger类
在我们处理数字时,如果数值过大时,我们无法处理,例如Integer,是 int的包装类,但int的最大值为2147483647,这时我们可以使用BigInteger类,他的数值范围比long还大的多,它支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何,大小的整数值而不会丢失任何信息。
import java.math.BigInteger;
public class Demo12 {
public static void main(String[] args) {
/*
BigInteger类
可以容纳比long,Integer跟大的数值
*/
System.out.println(Integer.MAX_VALUE);
System.out.println(Long.MAX_VALUE);
int a=2147483647;
long b=9223372036854775807l;
BigInteger b1=new BigInteger("214748364721474836472147483647214748364721474836472147483647");
BigInteger b2=new BigInteger("92233720368547758079223372036854775807");
BigInteger b3=b1.add(b2);
System.out.println(b3);
}
}
BigDecimal类
import java.math.BigDecimal;
public class Demo13 {
public static void main(String[] args) {
BigDecimal bigDecimal = new BigDecimal("16.9");
BigDecimal bigDecimal1 = new BigDecimal("10.9");
BigDecimal bigDecimal2 = bigDecimal.subtract(bigDecimal1);
System.out.println(bigDecimal2);
BigDecimal bigDecimal3 = new BigDecimal("15");
BigDecimal bigDecimal4 = new BigDecimal("9");
System.out.println(bigDecimal3.divide(bigDecimal4, 2, BigDecimal.ROUND_UP));
}
}