Python工程师Java之路(h)Java字符串拼接+null拼接处理
Posted 小基基o_O
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python工程师Java之路(h)Java字符串拼接+null拼接处理相关的知识,希望对你有一定的参考价值。
文章目录
Java版本:1.8
加号
String a = "ab" + 'c' + 5L;
System.out.println(a);
//abc5
String b = 9.99F + "ABC" + null;
System.out.println(b);
//9.99ABCnull
String的concat
System.out.println("aa".concat("bb"));
//aabb
StringBuilder和StringBuffer的append
StringBuilder stringBuilder = new StringBuilder("ab");
stringBuilder.append(99L).append("cd");
System.out.println(stringBuilder);
//ab99cd
StringBuffer stringBuffer = new StringBuffer("ab");
stringBuffer.append(9.99F).append("cd");
System.out.println(stringBuffer);
//ab9.99cd
StringBuffer是线程安全的(synchronized),源码截取如下
@Override
synchronized StringBuffer append(AbstractStringBuilder asb)
toStringCache = null;
super.append(asb);
return this;
StringBuilder的append不是线程安全的,速度更快,源码截取如下
public StringBuilder append(StringBuffer sb)
super.append(sb);
return this;
String的join
String a = String.join("-", "a", "b", "c");
System.out.println(a);
//a-b-c
String[] strings = "a", "b", "c", null, "e";
String b = String.join("-", strings);
System.out.println(b);
//a-b-c-null-e
源码截取如下,第一个参数为分隔符
public static String join(CharSequence delimiter, CharSequence... elements)
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
// Number of elements not likely worth Arrays.stream overhead.
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements)
joiner.add(cs);
return joiner.toString();
StringJoiner
StringJoiner实例化时,可设置(分隔符,前缀,后缀)
import java.util.StringJoiner;
public class Hi
public static void main(String[] args)
//StringJoiner(分隔符, 前缀, 后缀)
StringJoiner stringJoiner = new StringJoiner(",", "[", "]");
//遍历
String[] strings = "a", "b", "c", null, "e";
for (String string : strings)
//过滤空值
if (string != null)
stringJoiner.add(string);
//打印
System.out.println(stringJoiner.toString());
-
打印效果
- [a,b,c,e]
StringUtils.join
import com.sun.deploy.util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
public class Hi
public static void main(String[] args)
ArrayList<String> a = new ArrayList<>(Arrays.asList("a", "b", null, "d"));
//不过滤空值
System.out.println(StringUtils.join(a, ",")); //a,b,null,d
//过滤空值
a.removeIf(Objects::isNull);
System.out.println(StringUtils.join(a, ",")); //a,b,d
场景:拼接SQL
Phoenix(待完善)
import com.alibaba.fastjson.JSONObject;
import com.sun.deploy.util.StringUtils;
public class Hello
public static void main(String[] args)
String s = "\\"tb\\":\\"sku\\",\\"data\\":\\"id\\":\\"99\\",\\"name\\":\\"椰子\\"";
JSONObject js = JSONObject.parseObject(s);
String table = js.getString("tb");
JSONObject data = js.getJSONObject("data");
String sql = "upsert into " + table
+ " (" + String.join(",", data.keySet()) + ") "
+ "values"
+ " ('" + StringUtils.join(data.values(), "','") + "');";
System.out.println(sql);
打印结果
upsert into sku (name,id) values ('椰子','99');
mysql(待完善)
import com.alibaba.fastjson.JSONObject;
import java.util.StringJoiner;
public class Hi
public static void main(String[] args)
String s = "\\"tb\\":\\"sku\\",\\"data\\":\\"id\\":99,\\"name\\":\\"椰子\\"";
JSONObject js = JSONObject.parseObject(s);
String table = js.getString("tb");
JSONObject data = js.getJSONObject("data");
StringJoiner columns = new StringJoiner(",", "(", ")");
StringJoiner values = new StringJoiner(",", "(", ")");
for (String column : data.keySet())
columns.add(column);
Object value = data.get(column);
if (value.getClass() == String.class)
values.add("'" + value + "'");
else
values.add(value.toString());
System.out.println("INSERT " + table + columns + " VALUES " + values + ";");
打印结果
INSERT sku(name,id) VALUES ('椰子',99);
以上是关于Python工程师Java之路(h)Java字符串拼接+null拼接处理的主要内容,如果未能解决你的问题,请参考以下文章