字符串

字符串的常用方法

1
String str;
  1. 去掉左右多余空格
    str.trim()
    1
    2
    str.trim();
    // 方法执行后得到一个新的字符串,这个新的字符串左右没有空格
  2. 查找字符串 indexOf()
    1
    2
    3
    4
    str.indexOf(String string);
    // 返回文本中第一个匹配到索引值,return -1说明不匹配
    str.indexOf(String string, int start)
    // 从start位置开始查找
    1
    2
    3
    4
    String str = "sdfsd java sdfdsfsdf java fsdfsdf java";
    int index = str.indexOf("java");
    int result = str.indexOf("java", index + 4);
    // 查找第二个要查找的字符串;
  3. 字符串截取 substring()
    1
    2
    3
    4
    5
    str.substring(int start);
    // 从start索引(含start) -> 字符串结束

    str.substring(int start, int end);
    // 从start索引(含start) -> end索引(不含end)
  4. 字符串开始和结束内容判断 startsWith()/endsWith()
    1
    2
    3
    4
    5
    String file = "test.doc";
    file.endsWith(".doc");

    String url = "https://www.kkkkkk.com"
    url.startsWith("https");
  5. 字符串替换 replaceAll()
    1
    replace(String oldData, String newData);
  6. 字符串分割split()
    1
    2
    3
    4
    str.split(String string)
    // 以string为界分割字符串为数组(返回字符串数组)
    // 注意,分隔符为| * ^ : . 需要转义
    // eg. str.split("\\|");
  7. 大小写转换toUpperCase()/toLowerCase()
    1
    2
    str.toUpperCase() // 全部变为大写
    str.toLowerCase() // 全部变为小写
  8. 字符串比较equals()
    1
    2
    str.equals(String string);
    // 比较两个字符串 str 和 string 是否相同
  9. 数字和字符串转换Integer.parseInt()
    1
    2
    3
    4
    5
    int num = Integer.parseInt("12138");
    // 字符串转换为数字
    String string = String.valueOf(100);
    // 数字转换为字符串,valueOf()可以将数字、浮点、布尔类型转换为字符串
    // 也可以使用 "+" 拼接字符串自动转换数字为字符串

    对象

    日期类型

  • String、int、double、long这些属于基础类型,不需要导入包,日期类型LocalDate(java8版本推出)需import java.time.LocalDate
  • now() 返回当前程序所在计算机的系统时间
    1
    2
    3
    4
    5
    6
    7
    8
    9
    import java.time.LocalDate;

    public class DateTest {
    public static void main (String[] args) {
    LocalDate now = LocalDate.now();
    System.out.println(time.toString());
    // 2021-01-27
    }
    }

    日期时间和字符串的转化

  • yy-MM-dd字母是固定的,字母之外的字符可以更换
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;

    public class DateTest5 {

    public static void main(String[] args) {

    LocalDate time = LocalDate.now();
    System.out.println(time.toString());
    /* 2021-01-27 */

    /* 创建一个格式化方式 */
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
    String timeStr = df.format(time);
    System.out.println(timeStr);
    /* 2021年01月27日 */

    df = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    timeStr = df.format(time);
    System.out.println(timeStr);
    /* 2021/01/27 */
    }
    }

    获得日期具体值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import java.time.LocalDate;

    public class DateTest7 {

    public static void main(String[] args) {

    LocalDate time = LocalDate.now();

    // 得到当前时间所在年
    int year = time.getYear();
    System.out.println("当前年份 " + year);

    // 得到当前时间所在月
    int month = time.getMonth().getValue();
    System.out.println("当前月份 " + month);

    // 得到当前时间所在日
    int day = time.getDayOfMonth();
    System.out.println("当前日 " + day);

    // 得到当前时间所在星期数
    int dayOfWeek = time.getDayOfWeek().getValue();
    System.out.println("当前星期 " + dayOfWeek);

    }
    }
  • 关于getValue():
    • getMonth()getDayOfWeek()方法的返回值不是具体的数字,而是一个对象,所以必须使用getValue()得到具体的数字。

      时间日期类的运用

  1. 字符串转化为日期时间LocalDate.parse(String date, DateTimeFormatter df)第二个字段为可选字段,在字符串格式不是yyyy-MM-dd使用统一格式
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;

    public class DateTest8 {

    public static void main(String[] args) {

    String date = "2021-01-27";
    // 把字符串转化位 LocalDate 对象,并得到字符串匹配的时间
    LocalDate date2 = LocalDate.parse(date);
    // 打印出日期
    System.out.println(date2.toString());

    date = "2021/01/27";
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy/MM/dd");
    // 把字符串转化位 LocalDate 对象,并得到字符串匹配的日期
    LocalDate date2 = LocalDate.parse(date,df);
    // 打印出日期
    System.out.println(date2.toString());
    }
    }
  2. 时间日期的计算(eg. plusDays(int days) days为天数)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    import java.time.LocalDate;

    public class DateTest10 {

    public static void main(String[] args) {

    LocalDate now = LocalDate.now();
    System.out.println("当前:" + now.toString());

    System.out.println("加法运算");
    System.out.println("加1天:" + now.plusDays(1));
    System.out.println("加1周:" + now.plusWeeks(1));
    System.out.println("加1月:" + now.plusMonths(1));
    System.out.println("加1年:" + now.plusYears(1));

    System.out.println("减法运算");
    System.out.println("减1天:" + now.minusDays(1));
    System.out.println("减1周:" + now.minusWeeks(1));
    System.out.println("减1月:" + now.minusMonths(1));
    System.out.println("减1年:" + now.minusYears(1));
    }
    }
  3. 两个日期时间的判断
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import java.time.LocalDate;

    public class DateTest11 {

    public static void main(String[] args) {

    LocalDate now = LocalDate.now();

    // 可以对两个 LocalDateTime 进行比较,
    // 可以判断一个日期是否在另一个日期之前或之后,
    // 或者判断两个日期是否是同年同月同日。

    boolean isBefore = now.minusDays(1).isBefore(LocalDate.now());

    System.out.println("是否在当天之前:" + isBefore);

    boolean isAfter = now.plusDays(1).isAfter(LocalDate.now());
    System.out.println("是否在当天之后:" + isAfter);
    // 判断是否是当天
    boolean sameDate = now.isEqual(LocalDate.now());
    System.out.println("是否在当天:" + sameDate);
    }
    }