目录
字符串和编码
字符串
Java字符串的特点:
- 字符串对象可以直接使用"..."表示
- 内容不可变
- 使用equals()判断是否相等
字符串常用操作:
- 是否包含子串:contains/indexOf/lastIndexOf/startsWith/endsWith
- 去除首尾空白字符:trim
- 提取子串:substring
- 大小写转换:toUpperCase/toLowerCase
- 替换子串:replace/replaceAll
- 分割:split
- 拼接:join
String和其他数据类型互相转换 String和char[]互相转换 String和byte[]互相转换(需要指定编码)
编码
ASCII
GB2312/GBK/GB18030
Unicode/UTF-8
建议总是使用UTF-8编码
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
public class TestString {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "hello";
boolean a = s.equals("Hello");// "false"
// equalsIgnoreCase不考虑大小写
boolean b = s.equalsIgnoreCase("Hello");// true
// 移除首尾空白字符
String s1 = " \t hello \r \n";
String s2 = s.trim();// "hello"
s = s.trim();// "hello"
/**
* 是否包含子串
*/
String str = "hello";
str.contains("ll");// true
str.indexOf("ll");// 2
str.startsWith("he");// true
str.endsWith("lo");// true
// 提取子串
String str1 = "hello,world";
str1.substring(7, 9);// "or"
// 替换子串
String s3 = "hello";
s3.replace('l', 'w');// "hewwo"
s3.replace("l", "w~");// "hew~w~o"
// 正则表达式替换子串
String str2 = "A,,B,;C;,D";
str2.replaceAll("[\\,\\;\\s]+", ", ");// "A, B, C, D"
// static String join()拼接字符串
String[] arr = { "A", "B", "C" };
String s4 = String.join("~~", arr);// "A~~B~~C"
// 分割字符串
String str3 = "A,,B;C,D";
String[] ss = str3.split("[\\,\\;\\s]+");// {"A","B","C","D"}
/**
* 大小写转换
*/
String str4 = "hello";
str4.toUpperCase();
str4.toLowerCase();
// 把任意数据转换为String:
String.valueOf(123);// "123"
String.valueOf(true);// "true"
String x = String.valueOf(new Object());// "java.lang.Object@15db9742"
// 把String转换为其他类型
int i = Integer.parseInt("123");
Integer I = Integer.valueOf("123");
// ERROR
Integer I2 = Integer.getInteger("123");
// String转换为char[]
String s5 = "hello";
char[] cs = s.toCharArray();// {'h','e','l','l','o'}
// char[]转换为String
String s6 = new String(cs);
// String转换为byte[]
String s7 = "hello";
byte[] bs1 = s7.getBytes("UTF-8");
byte[] bs2 = s7.getBytes(StandardCharsets.UTF_8);
// byte[]转换为String
new String(bs1, "UTF-8");
new String(bs2, StandardCharsets.UTF_8);
}
}
StringBuilder
StringBuilder
- 可以高效拼接String
- 是可变对象
- 可以进行链式操作
不要使用StringBuffer(多线程)
public class StringBuilderTest {
public static void main(String[] args) {
String name = "World";
StringBuilder sb = new StringBuilder();
sb.append("Hello, ").append(name).append('!');
String s = sb.toString();
System.out.println(s);
}
}
包装类型
JDK定义的包装类型:
boolean | Boolean |
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
int、Integer和String的相互转换
自动装箱:auto boxing
自动拆箱:auto unboxing
自动拆箱可能发生NullPointerException
装箱和拆箱会影响执行效率
包装类型定义了一些有用的静态变量
JavaBean
JavaBean是一种Java编程规范:
目的:
- 方便IDE工具读写属性
- 传递数据
- 枚举属性
JavaBean特点:
一组public getter/setter方法
boolean属性的读方法通常为isXxx()
属性可以是只读或只写的
属性只需要getter/setter方法,不一定需要实例字段
利用IDE可以快速生成属性方法
枚举类 (Enumeration)
Java使用enum定义常量类型,常量本身带有类型信息,可以使用==比较。
enum定义的类型是class,继承自java.lang.Enum
所有常量都是唯一引用实例
常量可用于switch语句
name()获取常量定义的字符串,注意不要使用toString()
ordinal()返回常量定义的顺序(无实质意义)
可以为enum类编写构造方法、字段、方法
构造方法必须为private
常用工具类
-
Math:数学计算
-
Random:生成伪随机数
-
SecureRandom:生成安全的随机数
-
BigInteger:表示任意大小的整数
-
BigDecimal:表示任意精度的浮点数
package com.feiyangedu.sample;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// Math
Math.sqrt(2); // 1.414
Math.pow(2, 10);// 2^10=1024
Math.exp(2);// e^2=7.389
Math.log(4);// loge4=1.386
Math.log10(100);// 2
Math.sin(Math.PI / 6);// sin(PI/6)=0.5
Math.cos(Math.PI / 3);// 0.5
// Math.random() 0<=随机数<1
double x1 = Math.random();
// 生成某个区间的随机数 MIN<=R<MAX
long MIN = 1000;
long MAX = 9000;
double x2 = Math.random() * (MAX - MIN) + MIN;
double r = (long) x2;
// Random用来创建伪随机数 不给定种子时Random使用系统当前时间戳作为种子
Random rnd = new Random(123456);
rnd.nextInt();
rnd.nextFloat();
rnd.nextInt(10);// 生成不大于N的随机数
// SecureRandom 用来创建安全的随机数
SecureRandom sr = new SecureRandom();
for (int i = 0; i < 10; i++) {
System.out.println(rnd.nextInt(100));
}
// BigInteger 用任意多个int[]来表示非常大的整数
BigInteger bi = new BigInteger("1234567890");
System.out.println(bi.pow(5));
// BigDecimal 表示任意精度的浮点数
BigDecimal bd = new BigDecimal("123.10");
System.out.println(bd.multiply(bd));
}
}