import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil {
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
public static boolean isNotEmpty(String str){
return str!=null && str.trim().length()!=0;
}
public static boolean isNumber(String str) {
boolean isNumber = false;
String expr = "^[0-9]+$";
if (str.matches(expr)) {
isNumber = true;
}
return isNumber;
}
public static boolean isNumberLetter(String str) {
boolean isNoLetter = false;
String expr = "^[A-Za-z0-9]+$";
if (str.matches(expr)) {
isNoLetter = true;
}
return isNoLetter;
}
public static boolean isMobileNo(String str) {
boolean isMobileNo = false;
try {
Pattern p = Pattern.compile("^(13|14|15|17|18)\\d{9}$");
Matcher m = p.matcher(str);
isMobileNo = m.matches();
} catch (Exception e) {
}
return isMobileNo;
}
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
if(sb.indexOf("\n")!=-1 && sb.lastIndexOf("\n") == sb.length()-1){
sb.delete(sb.lastIndexOf("\n"), sb.lastIndexOf("\n")+1);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static List<String> parseString2ListByCustomerPattern(String pattern, String src) {
if (src == null) {
return null;
}
List<String> list = new ArrayList<String>();
String[] result = src.split(pattern);
for (int i = 0; i < result.length; i++) {
list.add(result[i]);
}
return list;
}
public static int toInt(String s) {
int result = 0;
if (!isEmpty(s)) {
try {
result = Integer.parseInt(s);
} catch (Exception e) {
}
}
return result;
}
public static long toLong(String s) {
long result = 0;
if (!isEmpty(s)) {
try {
result = Long.parseLong(s);
} catch (Exception e) {
}
}
return result;
}
public static Float toFloat(String s) {
float result = new Float(0);
if (!isEmpty(s)) {
try {
result = Float.parseFloat(s);
} catch (Exception e) {
}
}
return result;
}
public static double toDouble(String s) {
double result = 0;
if (!isEmpty(s)) {
try {
result = Double.parseDouble(s);
} catch (Exception e) {
}
}
return result;
}
public static String getUUID() {
return UUID.randomUUID().toString();
}
public static String getSimpleUUID() {
return UUID.randomUUID().toString().replace("-", "");
}
}