package com.omg.web.util;
import java.beans.PropertyDescriptor;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
public class StringUtil {
// 数字正则
private static final Pattern RE_NUMBER = Pattern.compile("[0-9]+");
// 字符正则
private static final Pattern RE_CHARACTER = Pattern.compile("\\w+");
private static final Pattern Html_TAG = Pattern.compile("<style.*?</style>|<script.*?</script>|<([^>]*)>");
// 千分位显示
private static final DecimalFormat THOUSANDS_TAG = new DecimalFormat("#,###");
public static String join(String[] strs, String s) {
if (null == strs)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
sb.append(strs[i]);
if (i != strs.length - 1)
sb.append(s);
}
return sb.toString();
}
public static String[] split(String str, char separatorChar) {
return StringUtils.split(str, separatorChar);
}
public static List<String> splitToList(String str, char separatorChar) {//
if (StringUtil.isBlank(str)) {
return null;
}
List<String> list = new ArrayList<String>();
for (String s : StringUtil.split(str, separatorChar)) {
list.add(s);
}
return list;
}
public static boolean isBlank(String str) {
if (null == str)
return true;
if ("".equals(str.replaceAll(" "," ").trim()))
return true;
return false;
}
public static String toRandom(int j) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < j; i++) {
Random r = new Random();
int n = r.nextInt(3);
if (n == 0) {
s.append(r.nextInt(9));
} else if (n == 1) {
s.append((char) ('a' + Math.random() * 26));
} else {
s.append((char) ('A' + Math.random() * 26));
}
}
return s.toString();
}
private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
public static String randomString(int length) {
if (length < 1) {
return null;
}
// Create a char buffer to put random letters and numbers in.
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbersAndLetters[new Random().nextInt(71)];
}
return new String(randBuffer);
}
public static boolean isMailAddr(String mailAddr) {
String check = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(mailAddr);
return matcher.matches();
}
public static boolean isNumber(String str) {
return (str != null) && RE_NUMBER.matcher(str).matches();
}
public static String getListToString(List<String> list, char separatorChar) {
if (null != list && list.size() > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i));
if (i != list.size() -1) {
sb.append(separatorChar);
}
}
return sb.toString();
}
return null;
}
/**
* 取出html里所有标签和<scritp><style>内容
*
* @param html html内容
* @param replace 替换内容
* @return
*/
public static String getHtmlToText(String html, String replace) {
Matcher m = Html_TAG.matcher(html);
return m.replaceAll(replace);
}
/**
* 判断是否只字符
*
* @param s
* @return
*/
public static boolean regularCharacter(String s) {
return RE_CHARACTER.matcher(s).matches();
}
/**
* 解析bean对象属性
*
* @param p
* @return
*/
public static String getDisplayName(PropertyDescriptor p) {
if (null != p && null != p.getReadMethod()) {
if (p.getPropertyType() == boolean.class) {
return p.getReadMethod().getName().substring(2);
}
return p.getReadMethod().getName().substring(3);
}
return "";
}
/**
* 获取千分位
*
* @param s
* @return ##,###.00
*/
public static String getThousands(String s) {
try {
return THOUSANDS_TAG.format(Double.valueOf(s));
} catch (Exception e) {
return "";
}
}
public static void main(String[] args) {
System.out.println(StringUtil.getThousands("2500000.000"));
}
}
import java.beans.PropertyDescriptor;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
public class StringUtil {
// 数字正则
private static final Pattern RE_NUMBER = Pattern.compile("[0-9]+");
// 字符正则
private static final Pattern RE_CHARACTER = Pattern.compile("\\w+");
private static final Pattern Html_TAG = Pattern.compile("<style.*?</style>|<script.*?</script>|<([^>]*)>");
// 千分位显示
private static final DecimalFormat THOUSANDS_TAG = new DecimalFormat("#,###");
public static String join(String[] strs, String s) {
if (null == strs)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
sb.append(strs[i]);
if (i != strs.length - 1)
sb.append(s);
}
return sb.toString();
}
public static String[] split(String str, char separatorChar) {
return StringUtils.split(str, separatorChar);
}
public static List<String> splitToList(String str, char separatorChar) {//
if (StringUtil.isBlank(str)) {
return null;
}
List<String> list = new ArrayList<String>();
for (String s : StringUtil.split(str, separatorChar)) {
list.add(s);
}
return list;
}
public static boolean isBlank(String str) {
if (null == str)
return true;
if ("".equals(str.replaceAll(" "," ").trim()))
return true;
return false;
}
public static String toRandom(int j) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < j; i++) {
Random r = new Random();
int n = r.nextInt(3);
if (n == 0) {
s.append(r.nextInt(9));
} else if (n == 1) {
s.append((char) ('a' + Math.random() * 26));
} else {
s.append((char) ('A' + Math.random() * 26));
}
}
return s.toString();
}
private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
public static String randomString(int length) {
if (length < 1) {
return null;
}
// Create a char buffer to put random letters and numbers in.
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbersAndLetters[new Random().nextInt(71)];
}
return new String(randBuffer);
}
public static boolean isMailAddr(String mailAddr) {
String check = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(mailAddr);
return matcher.matches();
}
public static boolean isNumber(String str) {
return (str != null) && RE_NUMBER.matcher(str).matches();
}
public static String getListToString(List<String> list, char separatorChar) {
if (null != list && list.size() > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i));
if (i != list.size() -1) {
sb.append(separatorChar);
}
}
return sb.toString();
}
return null;
}
/**
* 取出html里所有标签和<scritp><style>内容
*
* @param html html内容
* @param replace 替换内容
* @return
*/
public static String getHtmlToText(String html, String replace) {
Matcher m = Html_TAG.matcher(html);
return m.replaceAll(replace);
}
/**
* 判断是否只字符
*
* @param s
* @return
*/
public static boolean regularCharacter(String s) {
return RE_CHARACTER.matcher(s).matches();
}
/**
* 解析bean对象属性
*
* @param p
* @return
*/
public static String getDisplayName(PropertyDescriptor p) {
if (null != p && null != p.getReadMethod()) {
if (p.getPropertyType() == boolean.class) {
return p.getReadMethod().getName().substring(2);
}
return p.getReadMethod().getName().substring(3);
}
return "";
}
/**
* 获取千分位
*
* @param s
* @return ##,###.00
*/
public static String getThousands(String s) {
try {
return THOUSANDS_TAG.format(Double.valueOf(s));
} catch (Exception e) {
return "";
}
}
public static void main(String[] args) {
System.out.println(StringUtil.getThousands("2500000.000"));
}
}