com.bing.utils.StringParseUtil Maven / Gradle / Ivy
package com.bing.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import com.google.common.base.Strings;
/**
* @author shizhongtao
*
* date 2016-3-21
* Description: MayBe is's useful
*/
public class StringParseUtil {
/**
* Parses the string argument as a boolean. The {@code boolean} returned
* represents the value {@code true} if the string argument is not
* {@code null} and is equal, ignoring case, to the string {@code "true"},
* {@code "yes"},{@code "是"},{@code "a"} etc.
*
* Example: {@code Boolean.parseBoolean("True")} returns {@code true}.
* Example: {@code Boolean.parseBoolean("N")} returns {@code false}.
* Example: {@code Boolean.parseBoolean("No")} returns {@code false}.
*
* @param s
* the {@code String} containing the boolean representation to be
* parsed
* @return the boolean represented by the string argument
*/
public static boolean parseBoolean(String s) {
return ((!Strings.isNullOrEmpty(s) )&& toBoolean(s));
}
private static boolean toBoolean(String name) {
if (name.equalsIgnoreCase("false") || name.equalsIgnoreCase("no")) {
return false;
}
if (ArrayUtils.contains(new String[] { "否", "假", "N", "n", "0" }, name)) {
return false;
}
return true;
}
static final Pattern FLOATING_POINT_PATTERN = fpPattern();
static final Pattern FLOATING_POINT_PATTERN1 = Pattern
.compile("0[xX](?:\\p{XDigit}++(?:\\.\\p{XDigit}*+)?|\\.\\p{XDigit}++)");
static final Pattern CURRENCY_POINT_PATTERN = Pattern
.compile("(?<=^[$¥])(?:\\d++(?:\\.\\d*+)?|\\.\\d++)$");
static final Pattern FRACTION_POINT_PATTERN = Pattern
.compile("^-?(?:0|[1-9]\\d*)/-?[1-9]\\d*$");
static final Pattern PERCENT_POINT_PATTERN = Pattern
.compile("^(?:[1-9]\\d*(?:\\.\\d*+)?|0?\\.\\d++)(?=%$)");
private static Pattern fpPattern() {
String decimal = "(?:\\d++(?:\\.\\d*+)?|\\.\\d++)";
String completeDec = decimal + "(?:[eE][+-]?\\d++)?[fFdD]?";
String hex = "(?:\\p{XDigit}++(?:\\.\\p{XDigit}*+)?|\\.\\p{XDigit}++)";
String completeHex = "0[xX]" + hex + "[pP][+-]?\\d++[fFdD]?";
String fpPattern = "[+-]?(?:NaN|Infinity|" + completeDec + "|"
+ completeHex + ")";
return Pattern.compile(fpPattern);
}
/**
* Parses the specified string as a double-precision floating point value.
* The ASCII character {@code '-'} ('\u002D'
) is recognized
* as the minus sign.
*
* @param string
* the string representation of a {@code double} value
* @return the floating point value represented by {@code string}, or
* {@code null} if {@code string} has a length of zero or cannot be
* parsed as a {@code double} value
* @throws ParseException
*/
public static Double parseDouble(String string) throws ParseException {
if (StringUtils.isBlank(string)) {
return null;
}
if (FLOATING_POINT_PATTERN.matcher(string).matches()) {
// TODO(user): could be potentially optimized, but only with
// extensive testing
return Double.parseDouble(string);
}
// currency formater
Matcher matcher = CURRENCY_POINT_PATTERN.matcher(string);
if (matcher.find()) {
return Double.parseDouble(matcher.group());
}
// percent formater
matcher = PERCENT_POINT_PATTERN.matcher(string);
if (matcher.find()) {
char[] charArray = matcher.group().toCharArray();
int pIndex=charArray.length;
StringBuilder sb=new StringBuilder();
for (int i=0;i