com.bimface.sdk.utils.StringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bimface-java-sdk Show documentation
Show all versions of bimface-java-sdk Show documentation
Bimface provide the required call java sdk.
The newest version!
package com.bimface.sdk.utils;
import java.util.Collection;
import com.bimface.sdk.constants.BimfaceConstants;
/**
* 字符串工具
*
* @author bimface, 2016-06-01.
*/
public class StringUtils {
private StringUtils() {
}
/**
* @see #join(Object[] array, String sep, String prefix)
* @param array 需要连接的对象数组
* @param sep 元素连接之间的分隔符
* @return 连接好的新字符串
*/
public static String join(Object[] array, String sep) {
return join(array, sep, null);
}
/**
* @see #join(Object[] array, String sep, String prefix)
* @param list 需要连接的对象数组
* @param sep 元素连接之间的分隔符
* @return 连接好的新字符串
*/
public static String join(Collection> list, String sep) {
return join(list, sep, null);
}
/**
* @see #join(Object[] array, String sep, String prefix)
* @param list 需要连接的对象数组
* @param sep 元素连接之间的分隔符
* @param prefix 前缀字符串
* @return 连接好的新字符串
*/
public static String join(Collection> list, String sep, String prefix) {
Object[] array = list == null ? null : list.toArray();
return join(array, sep, prefix);
}
/**
* 以指定的分隔符来进行字符串元素连接
*
* 例如有字符串数组array和连接符为逗号(,)
* String[] array = new String[] { "hello", "world", "bimface", "cloud","storage" };
*
那么得到的结果是:
* hello,world,bimface,cloud,storage
*
*
*
* @param array 需要连接的对象数组
* @param sep 元素连接之间的分隔符
* @param prefix 前缀字符串
* @return 连接好的新字符串
*/
public static String join(Object[] array, String sep, String prefix) {
if (array == null) {
return "";
}
int arraySize = array.length;
if (arraySize == 0) {
return "";
}
if (sep == null) {
sep = "";
}
if (prefix == null) {
prefix = "";
}
StringBuilder buf = new StringBuilder(prefix);
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(sep);
}
buf.append(array[i] == null ? "" : array[i]);
}
return buf.toString();
}
/**
* 以json元素的方式连接字符串中元素
*
* 例如有字符串数组array
* String[] array = new String[] { "hello", "world", "bimface", "cloud","storage" };
*
那么得到的结果是:
* "hello","world","bimface","cloud","storage"
*
*
*
* @param array 需要连接的字符串数组
* @return 以json元素方式连接好的新字符串
*/
public static String jsonJoin(String[] array) {
int arraySize = array.length;
int bufSize = arraySize * (array[0].length() + 3);
StringBuilder buf = new StringBuilder(bufSize);
for (int i = 0; i < arraySize; i++) {
if (i > 0) {
buf.append(',');
}
buf.append('"');
buf.append(array[i]);
buf.append('"');
}
return buf.toString();
}
public static boolean isNullOrEmpty(String s) {
return s == null || "".equals(s);
}
public static boolean inStringArray(String s, String[] array) {
for (String x : array) {
if (x.equals(s)) {
return true;
}
}
return false;
}
public static boolean containsStringArray(String s, String[] array) {
for (String x : array) {
if (s.contains(x)) {
return true;
}
}
return false;
}
public static byte[] utf8Bytes(String data) {
return data.getBytes(BimfaceConstants.UTF_8);
}
public static String utf8String(byte[] data) {
return new String(data, BimfaceConstants.UTF_8);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy