友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

76. String

 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
27
28
29
30
31
32
33
34
35
36
37
package com.diguage.truman;

public class StringUtils {
  public static String switchFormat(int cur, int length) {
    String str = "" + cur;
    int q = length - str.length();
    switch (q) {
      case 0:
        break;
      case 1:
        str = "0" + str;
        break;
      case 2:
        str = "00" + str;
        break;
      case 3:
        str = "000" + str;
        break;
      case 4:
        str = "0000" + str;
        break;
      case 5:
        str = "00000" + str;
        break;
      case 6:
        str = "000000" + str;
        break;
      default:
        break;
    }
    return str;
  }

  public static String format(int cur, int len) {
    return String.format("%0" + len + "d", cur);
  }
}
 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
27
28
29
30
31
32
package com.diguage.truman;

import org.openjdk.jmh.annotations.*;

import java.util.concurrent.TimeUnit;

import static com.diguage.truman.StringUtils.format;
import static com.diguage.truman.StringUtils.switchFormat;

@BenchmarkMode(Mode.Throughput)
@Measurement(iterations = 10, time = 30)
@OutputTimeUnit(TimeUnit.SECONDS)
@Warmup(iterations = 3, time = 5)
@State(Scope.Thread)
public class StringUtilTest {

  // TODO 怎么书写测试用例?
  @Param({"1", "2"})
  int num;

  private static final int[] lens = {1, 2, 3, 4, 5, 6, 7};

  @Benchmark
  public String testStringFormat() {
    return format(num, num);
  }

  @Benchmark
  public String testSwitchFormat() {
    return switchFormat(num, num);
  }
}
 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.diguage.truman;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class StringTest {


  @Test
  public void testDedup() {
    List<String> lists = new ArrayList<>(1);
    for (int i = 0; i < Integer.MAX_VALUE; i++) {
      String is = String.valueOf(i);
      String s1 = "D瓜哥 · https://www.digauge.com".repeat(i % 10) + is;
      lists.add(new String(s1.substring(0, s1.length() - is.length())));
      String s2 = i + "D瓜哥 · https://www.digauge.com";
      lists.add(new String(s2.substring(is.length())));
      System.out.println(lists.size());
      if (i % 1000 == 0) {
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2L));
      }
    }
  }

  @Test
  public void testSplit() {
    String s = "abc";
    System.out.println(Arrays.toString(s.split("\\|")));


    // 调用 split 方法,如果参数为 null 时,则抛异常
    assertThatThrownBy(() -> s.split(null)).isInstanceOf(NullPointerException.class);
  }

  @Test
  public void testReplaceAll() {
    String value = "${abc} 是一个占位符,${abc}";
    Set<String> placeholders = getAllPlaceholders(value);

    for (String placeholder : placeholders) {
      System.out.println(value.replaceAll(placeholder, "ABC"));
    }
  }

  /**
   * 占位符正则表达式:${\w*}
   */
  private static final Pattern PH_PATTERN = Pattern.compile("(\\u0024\\{\\w*\\})+");

  private static Set<String> getAllPlaceholders(String value) {
    Matcher matcher = PH_PATTERN.matcher(value);
    Set<String> placeholders = new HashSet<>();
    int matcherStart = 0;
    while (matcher.find(matcherStart)) {
      String group = matcher.group();
      placeholders.add(group);
      matcherStart = matcher.end();
    }
    System.out.println(Arrays.toString(placeholders.toArray(new String[0])));
    return placeholders;
  }

}