博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java StringJoiner
阅读量:4699 次
发布时间:2019-06-09

本文共 2806 字,大约阅读时间需要 9 分钟。

Java StringJoiner

Java added a new final class StringJoiner in java.util package. It is used to construct a sequence of characters separated by a delimiter. Now, you can create string by passing delimiters like comma(,), hyphen(-) etc. You can also pass prefix and suffix to the char sequence.

import java.util.StringJoiner;public class StringJoinerExample {    public static void main(String[] args) {        // passing comma(,) as delimiter        StringJoiner joinNames = new StringJoiner(",");        // Adding values to StringJoiner        joinNames.add("Rahul");        joinNames.add("Raju");        joinNames.add("Peter");        joinNames.add("Raheem");        System.out.println(joinNames);        // passing comma(,) and square-brackets as delimiter        StringJoiner joinNames2 = new StringJoiner(",", "[", "]");        // Adding values to StringJoiner        joinNames2.add("Rahul");        joinNames2.add("Raju");        joinNames2.add("Peter");        joinNames2.add("Raheem");        System.out.println(joinNames2);        StringJoiner joinNames3 = new StringJoiner(",", "[", "]");        joinNames3.add("Rahul");        joinNames3.add("Raju");        StringJoiner joinNames4 = new StringJoiner(":", "[", "]");        joinNames4.add("Peter");        joinNames4.add("Raheem");        // Merging two StringJoiner        StringJoiner merge = joinNames3.merge(joinNames4);        System.out.println(merge);    }}

点击查看结果

Rahul,Raju,Peter,Raheem[Rahul,Raju,Peter,Raheem][Rahul,Raju,Peter:Raheem]
import java.util.StringJoiner;public class StringJoinerMethodExample {    public static void main(String[] args) {        StringJoiner joinNames = new StringJoiner(",");        // Prints nothing because it is empty        System.out.println(joinNames);        // We can set default empty value.        joinNames.setEmptyValue("It is empty");        System.out.println(joinNames);        // Adding values to StringJoiner        joinNames.add("Rahul");        joinNames.add("Raju");        System.out.println(joinNames);        // Returns length of StringJoiner        int length = joinNames.length();        System.out.println("Length: " + length);        // Returns StringJoiner as String type        String str = joinNames.toString();        System.out.println(str);        // Now, we can apply String methods on it        char ch = str.charAt(3);        System.out.println("Character at index 3: " + ch);        // Adding one more element        joinNames.add("Sorabh");        System.out.println(joinNames);        // Returns length        int newLength = joinNames.length();        System.out.println("New Length: " + newLength);    }}

点击查看结果

It is emptyRahul,RajuLength: 10Rahul,RajuCharacter at index 3: uRahul,Raju,SorabhNew Length: 17

参考资料

转载于:https://www.cnblogs.com/hglibin/p/10095770.html

你可能感兴趣的文章
在aws ec2上使用root用户登录
查看>>
数据访问 投票习题
查看>>
cnblog!i'm coming!
查看>>
使用点符号代替溢出的文本
查看>>
Axios 中文说明
查看>>
fatal: remote origin already exists.
查看>>
gridview 自定义value值
查看>>
2018二月实现计划成果及其三月规划
查看>>
类名.class和getClass()区别
查看>>
12/17面试题
查看>>
LeetCode 242. Valid Anagram
查看>>
JSP表单提交乱码
查看>>
如何适应现代雇佣关系
查看>>
团队项目(第五周)
查看>>
SQL 优化经验总结34条
查看>>
开源 视频会议 收藏
查看>>
核心J2EE模式 - 截取过滤器
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>