设计经典网站,寿光网站优化,专门做10s视频的网站,品牌设计网站1 字符串相关
1.1 String
所属包#xff1a;java.lang
代表不可变的字符序列 注意#xff1a;Java中#xff0c;String是一个final类 1#xff09;创建字符串方式
String a hello; // 开辟内存空间
String b new String(hello);
String d…1 字符串相关
1.1 String
所属包java.lang
代表不可变的字符序列 注意Java中String是一个final类 1创建字符串方式
String a hello; // 开辟内存空间
String b new String(hello);
String d new String(hello);
// 通过 拼接 生成新的字符串
String str 你好 世界;其他
String(byte[] bytes)
String(char[] value)
//参数为字节数组
byte[] bytes {97,98,99};
String str new String(bytes);
System.out.println(str); //参数为字符数组
char[] chars {a,b,c};
String str new String(chars);
System.out.println(str);
2常用方法
字符串的长度
length() 字符串的比较 方法 说明 boolean equals(String value) 比较两个字符串内容是否相同严格判等 boolean equalsIgnoreCase(String s) 比较两个字符串忽略大小写 int compareTo(String value) 按字典顺序比较两个字符串。 如果两个字符串相等则返回 0 如果字符串在参数值之前则返回值小于 0 如果字符串在参数值之后则返回值大于 0 int compareToIgnoreCase(String val) 按字典顺序比较两个字符串忽略大小写 boolean isEmpty() 判断是否是空字符串()
public class App2 {public static void main(String[] args) {// TODO Auto-generated method stubString s1 hello;// 获取字符串内容的长度int length s1.length();System.out.println(length);String s2 hello;String s3 world;// 比较字符串的内容是否相同boolean ret s1.equals(s2);System.out.println(ret);System.out.println(s1.equals(s3));String s4 Hello;System.out.println(s1.equals(s4));// 忽略大小写比较字符串内容boolean ret2 s1.equalsIgnoreCase(s4);System.out.println(ret2);String s6 HELLO;// 比较大小// 如果两个字符串一模一样返回0// 返回大于0的值前面的字符串比后面的字符串大// 返回小于0的值前面的字符串比后面的字符串小int ret3 s1.compareTo(s6);System.out.println(ret3);// 忽略大小写比较字符串的大小System.out.println(s1.compareToIgnoreCase(s6));// 内容是空String s7 ;boolean ret4 s7.isEmpty();System.out.println(ret4);System.out.println(s1.isEmpty());// null表示没有引用对象String s8 null;// s8.isEmpty(); // 报错}}注意 与 equals 的区别 public class App3 {public static void main(String[] args) {// TODO Auto-generated method stubString s1 hello;String s2 hello;// trueSystem.out.println(s1 s2);String s3 new String(hello);// 比较的是字符串的地址// falseSystem.out.println(s1 s3);// 比较的是字符串的内容System.out.println(s1.equals(s3)); }}
字符串的查找 方法 说明 boolean contains(String str) 判断字符串是否包含指定的字符串 public boolean startsWith(String str) 判断字符串是否以指定的前缀开始是返回true public boolean endswith(String str) 判断字符串是否以指定的后缀结束是返回true public int indexOf(String str) 返回指定子字符串str在此字符串中第一次出现的的索引值如果未出现则返回 -1。 public int lastIndexOf(String str) 返回指定子字符串str在此字符串中最后一次出现的索引值如果未出现则返回 -1。 public char charAt(int index) 指定索引index处的char值
public class App4 {public static void main(String[] args) {// TODO Auto-generated method stubString s1 give you color see see;// 多态 方法的参数是接口或者实现类传的实现类String的对象// 是否包含指定的字符串boolean ret s1.contains(you);System.out.println(ret);System.out.println(s1.contains(abc));// 是否以指定的字符串开头boolean ret2 s1.startsWith(gi);System.out.println(ret2);System.out.println(s1.startsWith(g));System.out.println(s1.startsWith(abc));// 是否以指定的字符串结尾System.out.println(s1.endsWith(see));System.out.println(s1.endsWith(abc));System.out.println(s1.endsWith(SEE));// 从前往后查找指定的字符串出现位置的索引 索引从0开始的, 返回-1 说明没有查找到int index s1.indexOf(see);System.out.println(index);System.out.println(s1.indexOf(abc));// 从后往前查找System.out.println(s1.lastIndexOf(see));// 返回指定索引处的字符char chars s1.charAt(2);System.out.println(chars);// 运行报错// System.out.println(s1.charAt(100));}}
字符串的转换 方法 说明 byte[] getBytes() 将字符串转换为字节数组 char[] toCharArray() 将字符串转换为字符数组 public String concat(String str) 将指定字符串str连接到此字符串的结尾组成一个新字符串 static String valueOf(int i) 等等 将指定类型的数据转换为字符串静态方法 public String toUpperCase(); 将此字符串中的所有字符都转换为大写 public String toLowerCase(); 将此字符串中的所有字符都转换为小写
public class App6 {public static void main(String[] args) {// TODO Auto-generated method stubString s1 hello;// 根据字符串获取字节数组byte[] bytes s1.getBytes();for(byte b : bytes) {System.out.println(b);}// 根据字符串得到字符数组char[] charArray s1.toCharArray();for(int i 0; i charArray.length; i) {System.out.println(charArray[i]);}// 字符串的连接拼接 作用类似 String s2 s1.concat(world);System.out.println(s2);System.out.println(s1);// 执行的数据转为字符串形式int i 10;String v1 String.valueOf(10);System.out.println(v1);System.out.println(String.valueOf(12.3));// 将字符串中所有字符转为大写String upperCase s1.toUpperCase();System.out.println(upperCase);// 转为小写System.out.println(upperCase.toLowerCase());}}
字符串的其他方法 方法 说明 public String substring(int index) 截取从索引index开始直到此字符串末尾的子字符串 public String substring(int start, int end) 截取指定开始索引和结束索引直接的字符串不包括结束索引位置的字符 public String[] split(String regex) 根据匹配规则拆分此字符串返回字符串数组注意特殊字符如\. static String join(CharSequence delimiter, CharSequence... elements) 使用指定的分隔符进行字符串的拼接 public String replace(char oldChar, char newChar) 返回用 newChar 替换此字符串中出现的所有 oldChar 而生成的新字符串 String replace(CharSequence old,CharSequence new) 使用新的字符串替换所有老的字符串CharSequence是接口String实现了该接口 public String trim() 去除字符串前后的空白字符
public class App7 {public static void main(String[] args) {// TODO Auto-generated method stubString info 窗前明月光,地上鞋两双;// 从指定的索引截取字符串截取到末尾String s1 info.substring(2);System.out.println(s1);System.out.println(info);// 截取,号后面的内容不能数索引int index info.indexOf(,);System.out.println(info.substring(index 1));// 根据开始和结束索引截取字符串注意不包含结束索引处的元素String s2 info.substring(2, 5);System.out.println(s2);// 根据指定字符串进行拆分String[] arr info.split(,);for(String s : arr) {System.out.println(s);}String info2 let house come;String[] arr2 info2.split( );System.out.println(arr2[0]);// 根据指定字符串进行其他字符串拼接String joinStr String.join(-, Java, iOS, php);System.out.println(joinStr);// 可变参数本质上就是数组System.out.println(String.join(:, arr2));System.out.println(String.join(, arr2));// 使用新的字符替换字符串中所有旧的字符String replace info2.replace(o, O);System.out.println(replace);// 根据字符串进行替换System.out.println(info2.replace(o, O));System.out.println(info2.replace(house, girl));// 去除字符串两端的空白字符空格 制表符 都属于空白字符String s hello ;String trim s.trim();System.out.println(trim);System.out.println(trim.length());String aa hel lo;String replace2 aa.replace( , );System.out.println(replace2);}}
1.2 StringBuffer
String对象的值是固定的不能改变内容。虽然可以使用来串联字符串达到附加新字符的目的但会产生一个新的String对象。
如果程序需要频繁连接字符串生成的中间对象会非常多带来内存消耗。此时可以使用可变字符串类StringBuffer (线程安全的)或StringBuilder StringBuffer类代表可变的字符序列
StringBuffer称为字符串缓冲区。它的工作原理是创建时申请一块内存存放字符序列随着字符内容的不断扩大如果缓冲区无法容纳增加的字符再重新改变缓存区的大小2倍2以容纳更多的字符序列
由于StringBuffer是可变对象在频繁进行字符串修改添加删除时首选StringBuffer效率更高 两个常用的构造方法。
构造一个不带字符的字符串缓冲区初始容量为 16 个字符
StringBuffer()构造一个字符串缓冲区并将其内容初始化为指定的字符串内容
StringBuffer(String str)
public class App1 {public static void main(String[] args) {// TODO Auto-generated method stub// 创建空字符串StringBuffer sb new StringBuffer();System.out.println(sb);// 根据指定字符串创建StringBuffer对象StringBuffer sb2 new StringBuffer(hello);System.out.println(sb2);}}常用方法 方法 说明 StringBuffer append(String str)等 将指定的字符串追加到此字符序列 还支持其他类型参数 StringBuffer insert(int offset, String str) 将字符串str插入此字符序列指定位置中 int length( ) 返回StringBuffer对象的长度 int indexOf(String str) 返回指定子字符串在该字符串中第一次出现的位置索引 String toString( ) 将此StringBuffer对象转换为字符串形式 StringBuffer reverse() 反转字符串 StringBuffer delete(int start, int end) 删除此字符串从 start 位置开始到 end (不包含)位置结束的字符序列 StringBuffer deleteCharAt(int pos) 将删除 pos 索引处的字符 StringBuffer replace(int start, int end, String s) 用指定字符串s替换字符串从 start 位置开始到 end 位置结束的字符序列
public class App2 {public static void main(String[] args) {// TODO Auto-generated method stubStringBuffer sb new StringBuffer();// 追加sb.append(hello);sb.append(10);sb.append(12.4);System.out.println(sb);// 指定索引处插入元素sb.insert(2, world);System.out.println(sb);// StringBuffer中字符串的实际长度System.out.println(sb.length());System.out.println(sb.capacity());sb.insert(5, 45.6);// 缓冲区的容量System.out.println(sb.capacity());System.out.println(sb);// 根据指定索引范围进行删除不包含结束索引处的元素sb.delete(2, 5);System.out.println(sb);// 删除指定索引处的字符sb.deleteCharAt(0);System.out.println(sb);// 替换指定索引范围的字符串为新的字符串不包含结束位置的字符sb.replace(2, 5, new);System.out.println(sb);// 字符串的反转 abc - cbasb.reverse();System.out.println(sb);}}
1.3 StringBuilder
非线程安全的可变字符串类
用法同StringBuffer但不保证线程的同步。如果对线程同步问题要求不是很高的话建议优先采用该类因为不用额外耗费性能维护线程的同步比 StringBuffer要快。 2 包装类
Java是面向对象的编程语言但Java中的基本数据类型并不是面向对象的为了解决这个不足Java为每个基本类型设计了一个对应的类称为包装类。
包装类是将基本类型封装到一个类中包含属性和方法方便对其进行操作位于java.lang包中
2.1 包装类介绍 基本类型 包装类 byte Byte short Short int Integer long Long char Character boolean Boolean float Float double Double
类关系图 2.2 自动装拆箱
JDK5.0之后Java为基本数据类型提供了自动装箱(boxing)、拆箱(unboxing)功能。
装箱将基本数据类型包装为对应的包装类对象
拆箱将包装类对象转换成对应的基本数据类型
// 装箱
Integer i 10;
// 拆箱
int i new Integer(10);public class App1 {public static void main(String[] args) {// TODO Auto-generated method stubint i 10;// 装箱, i2引用类型变量存的都是地址内容是20的Integer类型的对象的地址Integer i2 20;// 创建对象Integer i3 new Integer(30);// 拆箱 只要new肯定会创建对象将Integer对象的内容复制给int基础类型的变量int i4 new Integer(40);System.out.println(i2);System.out.println(i3);System.out.println(i4);Integer i5 20;// 和String类似的// trueSystem.out.println(i2 i5);// true 拆箱System.out.println(i2 20);// true 比较内容System.out.println(i2.equals(i5));Integer i6 1000;Integer i7 1000;// falseSystem.out.println(i6 i7);// true 拆箱System.out.println(i6 1000);// trueSystem.out.println(i6.equals(i7));// -128-127 的数据 会放到常量区// 有两个Integer类型的变量能否通过 比较值是否相同}} 2.3 包装类、基础数据、字符串之间转换
根据包装类获取基础类型 Integer i 10; int j i.intValue(); 等等 把字符串值转换为包装类对象 Integer.valueOf(String s) Double.valueOf(String s) 等等 把包装类对象转换为字符串 public String toString() String转换为基本数据类型 把字符串转换为doubleDouble.parseDouble(String s) 把字符串转换为intInteger.parseInt(String s) 等等
public class App2 {public static void main(String[] args) {// TODO Auto-generated method stub// 拆箱也可以实现包装类到基础类型的转换// 包装类-基础类型Integer i 10;int i1 i.intValue();Double d 12.3;double d2 d.doubleValue();// 字符串-包装类型String s 10;Integer i3 Integer.valueOf(s);System.out.println(i3);Double d3 Double.valueOf(12.3);System.out.println(d3);// 包装类 - 字符串String ss i.toString();System.out.println(ss);// 字符串- 基础类型int parseInt Integer.parseInt(100);double parseDouble Double.parseDouble(12.5);System.out.println(parseInt);System.out.println(parseDouble);}}
2.4 部分包装类用法
1Integer
// 转为二进制值 101
System.out.println(Integer.toBinaryString(5));// 转为16进制值
System.out.println(Integer.toHexString(12));// 转为8进制值
System.out.println(Integer.toOctalString(12));
2Character
char c1 9;
// 判断c1是否是数字
Character.isDigit(c1);
// 判断是否是字符(包括中文字符)
Character.isLetter(c1); char c3 A;
// 判断是否是小写字母
Character.isLowerCase(c3);
// 是否是大写字母
Character.isUpperCase(c3);
// 转换为小写字母
Character.toLowerCase(c3);
// 转换为大写字母
Character.toUpperCase(c3);3 日期与时间
3.1 Date
3.1.1 日期类
java.util.Date
// 获取当前时间
Date date new Date();
// 根据时间对象获取时间戳
date.getTime();
注时间戳单位毫秒表示1970-01-01 00:00:00到指定时间的毫秒数
3.1.2 日期对象与日期字符串转换
SimpleDateFormat类
方法 String format(Date date) 日期对象转为字符串 Date parse(String str) 日期字符串转为日期对象
SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
// 日期对象转为日期字符串
sdf.format(date);// 日期字符串转为日期对象
sdf.parse(dateStr)
日期格式字符串 yyyy 年 MM 月 dd 日 HH 小时24小时值 mm 分钟 ss 秒 SSS 毫秒 package com.renr;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class DateTest {public static void main(String[] args) {// TODO Auto-generated method stub// 获取当前时间Date date new Date();System.out.println(date);// 获取时间对象的时间戳System.out.println(date.getTime());// 时间格式化对象SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);// 日期类型对象转为时间字符串String dateStr sdf.format(date);System.out.println(dateStr);String dateStr2 2012-03-03 12:34:56;try {// 将日期字符串转为日期对象Date date2 sdf.parse(dateStr2);System.out.println(date2);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
3.2 Calendar
日历类
3.2.1 基本用法
获取日历对象Calendar.getInstance()
获取年月日等信息get()
设置年月日等信息set()
// 使用当前时区的日历对象
Calendar cal Calendar.getInstance();
System.out.println(年: cal.get(Calendar.YEAR));
// 月份从0开始
System.out.println(月: (cal.get(Calendar.MONTH) 1));
System.out.println(日: cal.get(Calendar.DAY_OF_MONTH));
System.out.println(时: cal.get(Calendar.HOUR_OF_DAY));
System.out.println(分: cal.get(Calendar.MINUTE));
System.out.println(秒: cal.get(Calendar.SECOND));Calendar cal2 Calendar.getInstance();
// 可以一次设置年月日时分秒注意月份的值
// cal.set(year, month, date, hourOfDay, minute, second);
cal2.set(2020, 1, 15, 23, 59, 59);
System.out.println(cal2);// 也可以分别设置
cal2.set(Calendar.YEAR, 2020);
cal2.set(Calendar.MONTH, Calendar.FEBRUARY);
cal2.set(Calendar.DAY_OF_MONTH, 12);
cal2.set(Calendar.HOUR_OF_DAY, 23);
cal2.set(Calendar.MINUTE, 59);
cal2.set(Calendar.SECOND, 59);
3.2.2 Date和Calendar转换
// Calendar转化为Date
Calendar cal Calendar.getInstance();
Date date cal.getTime();
System.out.println(date);// Date转化为Calendar
Date date2 new Date(1647271837481L);
Calendar cal2 Calendar.getInstance();
cal2.setTime(date2);
System.out.println(cal2);
3.2.3 时间运算
add方法
Calendar cal Calendar.getInstance();
System.out.println(cal.getTime());
// 日期的天 加减一个指定值
cal.add(Calendar.DAY_OF_MONTH, -1);
System.out.println(cal.getTime());
// 日期的月 加减一个指定值
cal.add(Calendar.MONTH, 1);
System.out.println(cal.getTime());
3.3 JDK8的日期时间类
LocalDateTime 日期时间类
LocalDate 日期类
LocalTime 时间类
获取当前日期、时间
// 获取当前日期 年月日
LocalDate localDate LocalDate.now();
// 获取当前时间 时分秒 毫秒
LocalTime localTime LocalTime.now();
// 获取当前的日期和时间
LocalDateTime localDateTime LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
根据指定日期/时间创建对象
// 指定年月日创建LocalDate对象
LocalDate localDate LocalDate.of(2022, 4, 13);
// 指定时分秒 创建LocalTime对象
LocalTime localTime LocalTime.of(22, 43, 20);
// 指定年月日时分秒创建LocalDateTime对象
LocalDateTime localDateTime LocalDateTime.of(2022, 4, 13, 22, 43, 20);
获取年月日时分秒
LocalDateTime localDateTime LocalDateTime.now();
// 获取当前日期是当年的第几天
int dayOfYear localDateTime.getDayOfYear();
// 获取当前日期是当月的第几天
int dayOfMonth localDateTime.getDayOfMonth();
// 获取当前日期是本周的星期几枚举类型
DayOfWeek dayOfWeek localDateTime.getDayOfWeek();//获取当前日期的年月日时分秒
// 获取年
int year localDateTime.getYear();
// 获取月
Month month localDateTime.getMonth();
// 获取日
int day localDateTime.getDayOfMonth();
// 获取时
int hour localDateTime.getHour();
// 获取分钟
int minute localDateTime.getMinute();
// 获取秒
int second localDateTime.getSecond();
日期时间的加减
LocalDateTime localDateTime LocalDateTime.now();// 指定日期添加指定的年数负数表示减少的年数
LocalDateTime plusYearsResult localDateTime.plusYears(2L);
// 指定日期添加指定的月数
LocalDateTime plusMonthsResult localDateTime.plusMonths(3L);
// 指定日期添加指定的天数
LocalDateTime plusDaysResult localDateTime.plusDays(7L);
// 指定日期添加指定的小时数
LocalDateTime plusHoursResult localDateTime.plusHours(2L);
// 指定日期添加指定的分钟数
LocalDateTime plusMinutesResult localDateTime.plusMinutes(10L);
// 指定日期添加指定的秒数
LocalDateTime plusSecondsResult localDateTime.plusSeconds(10L);// 另外的写法了解一下
LocalDateTime nextMonth localDateTime.plus(1, ChronoUnit.MONTHS);
LocalDateTime nextYear localDateTime.plus(1, ChronoUnit.YEARS);
LocalDateTime nextWeek localDateTime.plus(1, ChronoUnit.WEEKS);
时间日期前后的比较与判断
LocalDateTime localDateTime1 LocalDateTime.now();
LocalDateTime localDateTime2 LocalDateTime.of(2022, 3, 4, 12, 34,45);
// 前面的日期是否在指定日期之前
boolean ret localDateTime1.isBefore(localDateTime2);
// 前面的日期是否在指定日期之后
boolean ret2 localDateTime2.isAfter(localDateTime2);
// 前面的日期是否和指定日期相同
boolean ret3 localDateTime2.isEqual(localDateTime2);
字符串与日期对象转换
// 转为日期字符串
LocalDateTime date1 LocalDateTime.now();
// 指定日期格式
DateTimeFormatter formatter2 DateTimeFormatter.ofPattern(yyyy年MM月dd日 HH:mm:ss);
// 转为指定格式的日期字符串
String date2Str formatter2.format(date1);// 转为日期对象
String datetime 2022-04-13 21:47:30;
// 格式需要和日期字符串一致
DateTimeFormatter dtf DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);
// 日期字符串转为LocalDateTime对象
LocalDateTime ldt LocalDateTime.parse(datetime, dtf); 3.4 日期时间的工具类 4 其他
4.1 Math
Math类提供了一系列有关数学的基本运算和几何函数的方法位于java.lang包下
Math类是final类并且它的所有全局变量和方法都是静态的。
算数运算方法比较多 方法 说明 int abs (int a) 返回 int 值的绝对值 int max (int a, int b) 返回两个 int 值中较大的一个 int min (int a, int b) 返回两个 int 值中较小的一个 double sqrt (double a) 返回 double 值的正平方根 double pow(double a, double b) 返回第一个参数的第二个参数次幂的值 double floor (double a) 向下取整 double ceil(double a) 向上取整 long round(double b) 四舍五入 double random(); 返回一个随机的double值该值大于等于0.0小于1.0
// 平方根
System.out.println(Math.sqrt(16));
// 立方根
System.out.println(Math.cbrt(8.6));
// a的b次方
System.out.println(Math.pow(3, 2));
// 最大值
System.out.println(Math.max(4, 7));
// 最小值
System.out.println(Math.min(2.3, 4.5));
// 求绝对值
System.out.println(Math.abs(-10.7));
// 比指定数大的最小整数
System.out.println(Math.ceil(10.7));
System.out.println(Math.ceil(-1.7));
// 比指定数小的最大整数
System.out.println(Math.floor(10.7));
System.out.println(Math.floor(-10.7));
// 四舍五入为整数
System.out.println(Math.round(10.1));
System.out.println(Math.round(10.7));
System.out.println(Math.round(-10.7));
// [0,1)浮点数
System.out.println(Math.random());
4.2 Random类
创建对象
new Random() 方法 说明 public double nextDouble() 生成一个随机的double值数值介于[0,1.0)之间 public int nextInt() 生成一个随机的int值 public int nextInt(int n) 是生成一个随机的int值该值介于[0,n)的区间 4.3 System类 方法 说明 static long currentTimeMillis(); 返回当前的计算机时间的时间戳单位毫秒 static String getProperty(Stringkey) 获取指定的属性 static int exit(int status) 终止正在运行的程序status表示退出的状态码非零表示异常终止
String jversion System.getProperty(java.version);
String oName System.getProperty(os.name);
String user System.getProperty(user.name);
System.out.println(Java 运行时环境版本 jversion);
System.out.println(当前操作系统是 oName);
System.out.println(当前用户是 user);