Featured image of post 【合集】Java字符串String

【合集】Java字符串String

相关依赖

1
2
3
4
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
</dependency>
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>33.2.1-jre</version>
</dependency>

字符串命名风格转换

1
2
3
4
5
6
CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, "test-data");//testData
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "test_data");//testData
CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, "test_data");//TestData
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "testdata");//testdata
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "TestData");//test_data
CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, "testData");//test-data

字符串替换、字符替换

1
2
3
4
5
6
7
8
CharMatcher.breakingWhitespace().replaceFrom(" a \n\t\tb\r\nc ", '#'); // #a####b##c#
CharMatcher.whitespace().collapseFrom(" a \n\t\tb\r\nc ", '#'); // #a#b#c#
CharMatcher.whitespace().trimAndCollapseFrom(" a \n\t\tb\r\nc ", '#'); // a#b#c

CharMatcher.inRange('0', '9').retainFrom("abc 123 efg"); // 123
CharMatcher.inRange('0', '9').removeFrom("abc 123 efg"); // abc  efg

CharMatcher.inRange('a', 'z').or(CharMatcher.inRange('A', 'Z')).matches('K'); //true

字符串转List集合

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
List<String> transactionTypeList = StringUtils.isBlank(transactionType) || "all".equals(transactionType) ? Collections.emptyList() :
        Splitter.on(",").omitEmptyStrings().trimResults()
                .splitToList(transactionType);

// 将字符串转为集合
List<String> stringList = Splitter.on("-").omitEmptyStrings().trimResults()
        .splitToList("1-2-3-4- 5-  6  "); // [1, 2, 3, 4, 5, 6]
Map<String, String> stringStringMap = Splitter.on(",").omitEmptyStrings().trimResults().withKeyValueSeparator("=")
        .split("1=A,  2=B,  3=C "); // {1=A, 2=B, 3=C}

// 正则分隔字符串
Splitter.onPattern("[.|,]").omitEmptyStrings().trimResults()
        .splitToList(" aa.dd, ,ff,,. "); // [aa, dd, ff]

/**
 * 小写驼峰逗号字符串 转 小写下划线List集合 sort,createTime,id 转 [sort,create_time,id]
 */
List<String> strings = Splitter.on(",").omitEmptyStrings().trimResults()
      .splitToList(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, "sort,createTime,id"));

字符串转集合_转换_再拼接

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
String str = "1,2,3,4,5,6,7,8,9,10";
Map<Integer, String> map = new HashMap<>() {{
    put(1, "a");
    put(2, "b");
}};

str = null;

String string = Splitter.on(",")
        .omitEmptyStrings()
        .trimResults()
        .splitToList((str == null) ? "" : str)
        .stream()
        .map(s -> map.get(Integer.valueOf(s)))
        .filter(Objects::nonNull)
        .collect(Collectors.joining(";"));

// string 结果为 ""

字符串条件精准匹配

1
2
3
4
list = Optional.ofNullable(list).orElse(new ArrayList<>())
        .stream()
        .filter(s -> StringUtils.isBlank(searchString) || Objects.equals(s.getString(), searchString))
        .collect(Collectors.toList());

字符串条件模糊匹配

1
2
3
4
5
6
String regex = ".*" + Pattern.quote(searchString) + ".*"; // 使用Pattern.quote来转义特殊字符  
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); // 不区分大小写  
noteList = Optional.ofNullable(noteList).orElse(new ArrayList<>())
        .stream()
        .filter(e -> pattern.matcher(e.getPlateNumber()).matches())
        .collect(Collectors.toList());
Licensed under CC BY-NC-SA 4.0
皖ICP备2024056275号-1
发表了78篇文章 · 总计149.56k字
本站已稳定运行