1 字符串

image.png

  • StringBuilder 由于是非线程安全的,所以效率高一点
  • String 类是 immutable ,所以执行 += 可能带来效率问题 ```java import java.util.*;

public class StringAndStringBuffer {

  1. public static void main(String args[]) {
  2. String a = "1";
  3. String s = "";
  4. StringBuffer sb = new StringBuffer();
  5. final int N = 10000;
  6. long t0 = System.currentTimeMillis();
  7. for (int i = 0; i < N; ++i)
  8. s += a;
  9. long t1 = System.currentTimeMillis();
  10. for (int i = 0; i < N; ++i)
  11. sb.append(a);
  12. long t2 = System.currentTimeMillis();
  13. System.out.println(t1 - t0);
  14. System.out.println(t2 - t1);
  15. }

}

  1. > Java 中, `String` `+=` 被翻译为:先将 `String` 转换为 `StringBuilder` 对象,然后执行 `append()` ,最后执行 `toString()` 转化回 `String`
  2. <a name="levIR"></a>
  3. ## 1.1 String 常用方法
  4. 【**返回新的 **`**String**`** 对象**】
  5. - **连接 **`**concat()**`** **
  6. - **替换 **`**replace()**`** **
  7. - **替换全部 **`**replaceAll()**`** **
  8. - **返回子串 **`**substring()**`** **
  9. - **转化为小写 **`**toLowerCase()**`** **
  10. - **转化为大写 **`**toUpperCase()**`** **
  11. - **去掉空格 **`**trim()**`** **
  12. - **转换为字符串 **`**toString()**`** **
  13. **【查找】**
  14. - `**endsWith()**`** **
  15. - `**startsWith()**`** **
  16. - `**indexOf()**`** **
  17. - `**lastIndexOf()**`** **
  18. **【比较】**
  19. - `**equals()**`** **
  20. - `**equalsIgnoreCase()**`** **
  21. - `**compareTo()**`
  22. **【字符以及长度】**
  23. - `**charAt()**`** **
  24. - `**length()**`** **
  25. **【格式化】**
  26. - `**format()**`** **
  27. <br />
  28. <a name="izKoK"></a>
  29. ## 1.2 字符串常量(interned)
  30. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/805730/1611144241932-ccb0e4e1-d9cf-4557-a8cd-6501123e843d.png#crop=0&crop=0&crop=1&crop=1&height=154&id=fPf6i&margin=%5Bobject%20Object%5D&name=image.png&originHeight=307&originWidth=1177&originalType=binary&ratio=1&rotation=0&showTitle=false&size=302762&status=done&style=none&title=&width=588.5)
  31. <a name="JT6Ta"></a>
  32. ## 1.3 StringBuffer 类
  33. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/805730/1611144311112-b9d743bc-76e2-4a56-b905-7f05598089d6.png#crop=0&crop=0&crop=1&crop=1&height=248&id=hMdQq&margin=%5Bobject%20Object%5D&name=image.png&originHeight=495&originWidth=961&originalType=binary&ratio=1&rotation=0&showTitle=false&size=422366&status=done&style=none&title=&width=480.5)
  34. > `String` 的这些方法都会返回一个 `new` 的对象,而 `StringBuffer` 不会, `StringBuffer` 更像是 C++ 中的容器。
  35. <a name="42INY"></a>
  36. ## 1.4 字符串的分割
  37. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/805730/1611144427779-92564717-4445-43e8-8029-c1b7f5283b26.png#crop=0&crop=0&crop=1&crop=1&height=249&id=cZh50&margin=%5Bobject%20Object%5D&name=image.png&originHeight=498&originWidth=1195&originalType=binary&ratio=1&rotation=0&showTitle=false&size=523634&status=done&style=none&title=&width=597.5)
  38. ```java
  39. import java.util.*
  40. class TestStringTokenizer {
  41. public static void main(String[] args) {
  42. StringTokenizerst = new StringTokenizer("this is a test", " ");
  43. while (st.hasMoreTokens()) {
  44. System.out.println(st.nextToken());
  45. }
  46. st = new StringTokenizer("253,197,546", ",");
  47. double num = 0;
  48. while (st.hasMoreTokens()) {
  49. sum += Double.parseDouble(st.nextToken());
  50. }
  51. System.out.println(sum);
  52. }
  53. }

2 日期与时间

  • 日期格式化
  • System.currentTimeMillis()可以获取时间戳,单位是秒
  • Long.parseLong(string)可以将 String 中的长整型提取出来 ```java package com.lht.date;

import java.text.SimpleDateFormat; import java.util.Date;

public class CookieServlet extends HttpServlet { public static void main() { // Date date = new Date(); // 无参默认创建当前时间的 Date 对象 String now = System.currentTimeMillis() + “”; // 获取当前时间戳并转换成字符串 Date date = new Date(Long.parseLong(now)); SimpleDateFormat f = new SimpleDateFormat(“yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒”); System.out.println(f.format(date)); } }

  1. ```java
  2. 今天是 2022 年 7 月 16 日 星期六 20 点 50 分 23 秒