java中用String.Join美化代码的实例讲解
作者:小妮浅浅 时间:2022-03-04 08:17:04
我们在java中处理字符串的时候,一般会选择String,在python中同样也是作用于字符串。那么我们今天延伸一下它的用法,只使用String作用于代码,会发生什么样的神奇效果呢?接下来我们使用String.Join对代码进行美化,下面一起看看怎么操作吧。
1.jadk1.8为我们提供了String.join()方法
2.几个使用的例子。
让我们在项目灵活的使用它,使代码更加优美
package com.niu.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @description: String.Join的使用
* @createDate: 2020/12/19 1:58 下午
* @updateDate: 2020/12/19 1:58 下午
* @updateRemark:
* @version: 1.0
**/
public class Main {
public static void main(String[] args) {
String[] text = {"hello", "word","are","you","ok","?"};
pt(String.join(",", text)); //hello,word,are,you,ok,?
List<String> sList = new ArrayList<>();
sList.add("a");
sList.add("b");
sList.add("c");
pt(String.join("-", sList)); //a-b-c
//在多个字符串对象中间加入连接字符
pt(String.join("\t","I","am","a","student")); //I am a student
pt(String.join("\t \n >>>:","string","a","b","c"));
//string
// >>>:a
// >>>:b
// >>>:c
}
public static void pt(Object o) {
System.out.println(o);
}
}
来源:https://www.py.cn/java/jichu/22204.html
标签:java,String.Join,美化代码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Kotlin语言编程Regex正则表达式实例详解
2023-06-22 02:06:29
详解java.lang.reflect.Modifier.isInterface()方法
2023-07-27 18:25:25
java数据结构-堆实现优先队列
2023-11-25 08:30:20
![](https://img.aspxhome.com/file/2023/5/59335_0s.jpg)
详解Spring MVC CORS 跨域
2023-11-25 08:04:37
java关键字static的使用详解
2023-09-24 05:04:11
![](https://img.aspxhome.com/file/2023/1/58181_0s.jpg)
IntelliJ IDEA2019实现Web项目创建示例
2023-06-05 00:29:33
![](https://img.aspxhome.com/file/2023/3/62333_0s.jpg)
浅谈三分钟学习Java泛型中T、E、K、V、?的含义
2022-09-01 20:12:38
![](https://img.aspxhome.com/file/2023/2/64372_0s.jpg)
spring cglib 与 jdk 动态代理
2021-07-19 20:28:43
SpringBoot和Swagger结合提高API开发效率
2023-11-25 01:23:16
三步轻松搭建springMVC框架
2023-02-01 05:38:34
![](https://img.aspxhome.com/file/2023/6/64036_0s.jpg)
关于@ConditionalOnProperty的作用及用法说明
2023-11-24 02:39:19
Spring bean的实例化和IOC依赖注入详解
2023-11-23 23:57:15
java实现mongodb的数据库连接池
2023-11-23 14:23:09
Java8新特性之类型注解_动力节点Java学院整理
2023-10-10 16:13:07
java制作简单验证码功能
2022-12-07 02:12:46
![](https://img.aspxhome.com/file/2023/5/60835_0s.jpg)
java Long类型转为json后数据损失精度的处理方式
2022-08-11 12:37:43
![](https://img.aspxhome.com/file/2023/8/61518_0s.png)
java8 实现提取集合对象的每个属性
2023-10-17 19:37:27
Java中的内部类你了解吗
2022-12-11 08:29:10
![](https://img.aspxhome.com/file/2023/0/62430_0s.png)
关于java关键字this和super的区别和理解
2022-08-01 14:33:09
![](https://img.aspxhome.com/file/2023/2/63052_0s.png)
Java 实现判定顺序表中是否包含某个元素(思路详解)
2023-09-11 01:03:04
![](https://img.aspxhome.com/file/2023/4/58474_0s.png)