package com.css.common.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 用于操作字符串的工具类
*
* @version 1.0
*
*/
public class FormatUtil {
/**
* 过滤空格
* @param str
* @return
*/
public static String trim(String str) {
String temp = "";
if(str==null){
return temp;
}
for (int i = 0; i < str.length(); i++) {
temp = (new StringBuilder()).append(temp).append(str.substring(i, i + 1).trim()).toString();
}
return temp;
}
/**
* 判断对象是否为空
* @param str
* @return
*/
public static boolean isNotNull(Object str) {
boolean b = false;
if (str == null) {
return b;
}
if (str instanceof String) {
String value = str.toString();
if (value != null && trim(value).length() > 0) {
b = true;
}
} else if (str instanceof Double) {
Double value = (Double) str;
if (value != null && value.doubleValue() > 0) {
b = true;
}
} else if (str instanceof Integer) {
Integer value = (Integer) str;
if (value != null && value.intValue() > 0) {
b = true;
}
} else if (str instanceof Long) {
Long value = (Long) str;
if (value != null && value.longValue() > 0) {
b = true;
}
} else if (str instanceof String[]) {
String value[] = (String[]) str;
if (value != null && value.length > 0) {
b = true;
}
} else if (str instanceof Long[]) {
Long value[] = (Long[]) str;
if (value != null && value.length > 0) {
b = true;
}
} else if (str instanceof Byte) {
Byte value = (Byte) str;
if (value != null && value.byteValue() > 0) {
b = true;
}
} else if (str instanceof Map) {
Map value = (Map) str;
if (str != null && value.size() > 0) {
b = true;
}
} else if (str instanceof List) {
List value = (List) str;
if (str != null && value.size() > 0) {
b = true;
}
} else if (str != null) {
b = true;
}
return b;
}
/**
* Splits a string into substrings based on the supplied delimiter
* character. Each extracted substring will be trimmed of leading
* and trailing whitespace.
*
* @param str The string to split
* @param delimiter The character that delimits the string
* @return A string array containing the resultant substrings
*/
public static final List split(String str, char delimiter) {
// return no groups if we have an empty string
if ((str == null) || "".equals(str)) {
return new ArrayList();
}
ArrayList parts = new ArrayList();
int currentIndex;
int previousIndex = 0;
while ((currentIndex = str.indexOf(delimiter, previousIndex)) > 0) {
String part = str.substring(previousIndex, currentIndex).trim();
parts.add(part);
previousIndex = currentIndex + 1;
}
parts.add(str.substring(previousIndex, str.length()).trim());
return parts;
}
}