文章内容
一、字符串拼接
1、问题
有一个包含多个元素的字符串数组/集合,现在想将它拼接为以逗号分隔的字符串。
2、解决方案
jdk8开始提供了 String.join 这个API,可以用于使用固定分隔符拼接字符串,如:
1 2 3 4 5 | List<String> list = Arrays.asList( "1" , "2" , "3" ); String join = String.join( "," , list); // 1,2,3 String[] array = { "1" , "2" , "3" }; join = String.join( "," , array); // 1,2,3 |
3、讨论
String.join是jdk1.8版本新加入的API,是使用StringJoiner来实现的,StringJoiner是1.8新加入的用于构造由固定分隔符分隔的字符序列的工具,还可以为它提供一个前缀/后缀。可以显式地使用StringJoiner来完成字符串的拼接,如:
1 2 3 | StringJoiner sj = new StringJoiner( ":" , "[" , "]" ); sj.add( "George" ).add( "Sally" ).add( "Fred" ); String desiredString = sj.toString(); // [George:Sally:Fred] |
由于String.join方法和StringJoiner都只能拼接字符串数组/集合,如果需要拼接其他类型的数组/集合时,应该使用Stream先转为字符串的数组/集合,再进行拼接,如:
1 2 3 4 5 6 7 8 9 | List<Integer> numbers = Arrays.asList( 1 , 2 , 3 , 4 ); String commaSeparatedNumbers = numbers.stream() .map(Object::toString) .collect(Collectors.joining( ", " )); // 1, 2, 3, 4 int [] numberArray = { 1 , 2 , 3 , 4 }; commaSeparatedNumbers = Stream.of(numberArray) .map(Objects::toString) .collect(Collectors.joining( ", " )); // 1, 2, 3, 4 |
String.join/StringJoiner的分隔符可以是任意字符序列,即可以是多个字符。
二、生成随机字符串
1、问题
生成只包含字母和数字的随机N位数编号。
2、解决方案
可以使用apache-commons-lang3的RandomStringUtils工具类,或者使用hutool的RandomUtil工具类。
使用apache-commons-lang3的RandomStringUtils工具类:
1 | String random = RandomStringUtils.randomAlphanumeric( 6 ); |
使用hutool的RandomUtil工具类:
1 | String random = RandomUtil.randomString( 6 ); |
3、讨论
这两个工具类实际使用效果上稍微有点区别:
- apache-commons-lang3工具类默认是从大写字母、小写字母和数字中随机,hutool默认只有小写字母(或者使用randomStringUpper方法只有大写字母)。
- apache-commons-lang3工具类的执行效率高一些,hutool工具类的可以通过重载方法自己指定从哪些字符中进行随机。
hutool是一个值得安利的工具包,包含丰富的工具类,能满足多数日常场景的开发。参照:
Java工具类库Hutool
三、字符串模板渲染
1、问题
通过模板渲染的方式完成字符串的拼接。
2、解决方案
使用String.format:
1 2 3 | String message = String.format( "您好%s,晚上好!您目前余额:%.2f元,积分:%d" , "张三" , 10.155 , 10 ); System.out.println(message); //您好张三,晚上好!您目前余额:10.16元,积分:10 |
使用MessageFormat.format:
1 2 3 | String message = MessageFormat.format( "您好{0},晚上好!您目前余额:{1,number,#.##}元,积分:{2}" , "张三" , 10.155 , 10 ); System.out.println(message); //您好张三,晚上好!您目前余额:10.16元,积分:10 |
使用Freemarker模板引擎:
01 02 03 04 05 06 07 08 09 10 11 12 13 | try { map = new HashMap(); map.put( "name" , "张三" ); map.put( "money" , 10.155 ); map.put( "point" , 10 ); Template template = new Template( "strTpl" , "您好${name},晚上好!您目前余额:${money?string(" #.## ")}元,积分:${point}" , new Configuration( new Version( "2.3.23" ))); StringWriter result = new StringWriter(); template.process(map, result); System.out.println(result.toString()); //您好张三,晚上好!您目前余额:10.16元,积分:10 } catch (Exception e){ e.printStackTrace(); } |
3、讨论
- String.format是C语言风格的字符串模板渲染
- MessageFormat.format中如果需要使用左大括号'{‘,需要使用单引号包围;如果需要使用单引号,需要写两个单引号。
01 02 03 04 05 06 07 08 09 10 11 12 13 14 | // 添加一个单引号在占位符前,占位符失败 String str = MessageFormat.format( "This is '{0} {1} play!" , "why" , "we" ); System.out.println(str); // 占位符添加一对单引号,单个占位符失败 String str2 = MessageFormat.format( "This is '{0}' {1} play!" , "why" , "we" ); System.out.println(str2); // 占位符添加两个单引号,正常使用 String str3 = MessageFormat.format( "This is ''{0}'' {1} play!" , "why" , "we" ); System.out.println(str3); // 结果: // This is {0} {1} play! // This is {0} we play! // This is 'why' we play! |