02-String

1. 特殊字符

1. \

\\ 相当于 \
\\\\ 相当于 \\
image-20230911105814650
  • replaceAll()中,必须用\\\\表达一个\,因为Java中\是转义字符,通常会误以为\\就能表示\
  • 实际上,replaceAll()底层运用了正则表达式,经历了两次转化,即replaceAll("\\\\")转化出\\提交给正则表达式,正则表达式再转化一次获得\,同理,用8个反斜杠表示\\
.replaceAll("\\\\", "\\\\\\\\")

2. \u0000

// s 为unicode字符, 并不等于""
String s = "\u0000";
System.out.println("li" + s + "song");
System.out.println(new Gson().toJson("li" + s + "song"));
image-20230911105823039

2. byte[]

string和byte[]的转换open in new window

// string 转 byte[]
String str = "Hello";
byte[] srtbyte = str.getBytes();

// byte[] 转 string
String res = new String(srtbyte);


 


 

3. split

String s = "段1:00:00:00-12:12:12";
String[] split = s.split(":", 2);
for (String one : split) {
    System.out.println(one);
}


 


1
00:00:00-12:12:12

4. loop拼接

List<Ooxx> list = List.of(
        new Ooxx(1, "张三"),
        new Ooxx(2, "李四"),
        new Ooxx(3, "王五"));

StringBuilder sb = new StringBuilder();
for (Ooxx one : list) {
    sb.append(one.getName()).append(",");
}
sb.deleteCharAt(sb.length() - 1);
System.out.println("sb = " + sb);





 





List<Ooxx> list = List.of(
        new Ooxx(1, "张三"),
        new Ooxx(2, "李四"),
        new Ooxx(3, "王五"));

String collect = list.stream()
        .map(Ooxx::getName)
        .collect(Collectors.joining(","));
System.out.println("collect = " + collect);

3. StrUtil

1. 常量

image-20230911100012609
/**
 * 字符串常量:换行符 {@code "\n"}
 */
String LF = "\n";

2. hasBlank(), hasEmpty()

  • hasEmpty()只判断是否为null""
  • hasBlank()则会把" "也算做空
boolean hasBlank = StrUtil.hasBlank("li", "song", "tao", "");

3. concat()

// one|two|three|fore
String concat = StrUtil.concat(true, "one|", "two|", "three|", "fore");

4. format()

String template = "{}爱{},就像老鼠爱大米";
String str = StrUtil.format(template, "我", "你"); // str -> 我爱你,就像老鼠爱大米

5. split(), join()

String s = "<img src=http://listao.cn:10000/i/0/2024/04/08/084409-0.webp />";
List<String> split = StrUtil.split(s, StrUtil.SLASH);
System.out.println(split);
// [<img src=http:, , listao.cn:10000, i, 0, 2024, 04, 08, 084409-0.webp , >]
String join = StrUtil.join("\\", split);
System.out.println(join);
// <img src=http:\\listao.cn:10000\i\0\2024\04\08\084409-0.webp \>

6. sub

1. sub()

String s = "<img src=http://listao.cn:10000/i/0/2024/04/08/084409-0.webp alt=image-20200704111417472 alt=FileWriter />";
String sub = StrUtil.sub(s,0, 7);
System.out.println(sub);
// <img sr

1. subAfter()

String s = "<img src=http://listao.cn:10000/i/0/2024/04/08/084409-0.webp alt=image-20200704111417472 alt=FileWriter />";
String subAfter = StrUtil.subAfter(s, "alt=", true);
System.out.println(subAfter);
// image-20200704111417472 alt=FileWriter />

2. subBefore()

String s = "<img src=http://listao.cn:10000/i/0/2024/04/08/084409-0.webp alt=image-20200704111417472 alt=FileWriter />";
String subBefore = StrUtil.subBefore(s, "alt=", false);
System.out.println(subBefore);
// <img src=http://listao.cn:10000/i/0/2024/04/08/084409-0.webp alt=image-20200704111417472