📄 stringutil.java
字号:
package cn.bestwiz.jhf.admin.common.util;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 对字符串的一些变换的工具类
*
*
* @author wenyi <jhf@bestwiz.cn>
*
* @copyright 2006, BestWiz(Dalian) Co.,Ltd
* @version $Id: StringUtil.java,v 1.3 2008/05/26 07:50:30 luyb Exp $
*/
public class StringUtil {
public static Locale GlobalLocale = Locale.JAPAN;
public static final String COMMA = ",";
public static final String COLON = ":";
public static final String DOT = ".";
public static final String BASE_CURRENCY = "JPY";
public static final String USD_CURRENCY = "USD";
public static final String NZD_CURRENCY = "NZD";
public static final String NZD_JPY_CURRENCYPAIR = "NZD/JPY";
public static final String SLASH = "/";
// ALL
public static final String ALL = "ALL";
// AUTO
public static final String AUTO = "AUTO";
// -
public static final String POSITION_HYPHEN = "-";
// -
public static final String MINUS_HYPHEN = "-";
// 0
public static final String ZERO = "0";
// --
public static final String DOUBLE_HYPHEN = "--";
// -
public static final String PRICE_HYPHEN = " - ";
// TOTAL
public static final String TOTAL = "Total";
// system
public static final String SYSTEM_ID = "System";
// HEDGE
public static final String DL_PROPERTY_TYPE_HEDGE = "HEDGE";
// AUTO_HEDGE
public static final String DL_PROPERTY_KEY_HEDGE = "AUTO_HEDGE";
/**
* if num == null or "", change it to "---"
*
* @param num
* @return
*/
public static String formatPositionNum(BigDecimal num) {
String retStr = "";
if (num != null) {
retStr = num + "";
} else {
retStr = "---";
}
return retStr;
}
/**
* if num == null , change it to "0"
*
* @param num
* @return
*/
public static String filterNullToZero(Object num) {
String retStr = "";
if (num == null) {
retStr = "0";
} else {
retStr = num.toString();
}
return retStr;
}
/**
* if num == null , change it to ""
*
* @param num
* @return
*/
public static String filterNullToEmpty(Object num) {
String retStr = "";
if (num == null) {
retStr = "";
} else {
retStr = num.toString();
}
return retStr;
}
/**
* BigDecimal转换成boolean #1:true #0:false
*
* @param BigDecimal
* @return boolean
*/
public static boolean bigDecimalToboolean(BigDecimal showColTradedtime) {
if (showColTradedtime.intValue() == 0) {
return false;
} else {
return true;
}
}
/**
* boolean转换成BigDecimal #true:1 #false:0
*
* @param boolean
* @return BigDecimal
*/
public static BigDecimal booleanToBigDecimal(boolean selection) {
if (selection) {
return new BigDecimal(1);
} else {
return new BigDecimal(0);
}
}
public static void main(String[] argv) {
// String a = " this is a {0}, not a {1}";
// String b = MessageFormat.format( a , new String [] {"sample",
// "test"});
// System.out.println(b);
Pattern pattern = null;
// switch (type) {
// case NUMBER_WITHOUT_MINUS_TYPE:
// pattern = Pattern.compile("[0-9]\\d*");
// break;
//
// case NUMRIC_WITHOUT_MINUS_TYPE:
// pattern = Pattern.compile("[0-9.]\\d*");
// break;
//
// case NUMBER_WITH_MINUS_TYPE:
pattern = Pattern.compile("-[0-9]\\d*.[0-9]\\d*");
// break;
//
// case NUMRIC_WITH_MINUS_TYPE:
// pattern = Pattern.compile("-[0-9.]\\d*");
// break;
//
// case NUMBER_OR_CHAR_TYPE:
// pattern = Pattern.compile("[0-9A-Za-z]\\d*");
// break;
//
// default:
// break;
// }
Matcher matcher = pattern.matcher("-19.2222");
if (matcher.matches())
System.out.println("match");
String s = "GBP/JPY,USD/JPY,0|EUR/USD,EUR/GBP,0";
System.out.println(s.split("\\|"));
String[] ss = Pattern.compile("\u007C").split(s);
System.out.println(ss.length);
// System.out.println(ArrayUtils.toString());
}
/**
* check whether the String is a valid number
*
* @param param
* String to check
* @return true:valid ; false:invalid
*/
public static boolean checkNumberString(String param) {
boolean retValue = false;
if (param != null && !param.equals("")) {
try {
retValue = true;
} catch (NumberFormatException nfe) {
// do nothing
}
}
return retValue;
}
/**
* check whether the String equals ""
*
* @param param
* String to check
* @return true:notEmpty ; false:empty
*/
public static boolean notEmpty(String param) {
boolean retValue = false;
if (param != null && !param.equals("")) {
retValue = true;
}
return retValue;
}
public static String[] combineStringArrays(String[] front, String[] behind) {
String[] retValue = null;
if (front != null && behind != null) {
retValue = new String[front.length + behind.length];
int i = 0;
for (i = 0; i < front.length; i++) {
retValue[i] = front[i];
}
for (int j = 0; j < behind.length; j++, i++) {
retValue[i] = behind[j];
}
}
return retValue;
}
public static String bookingFormatNumberToStandard(String s) {
if (s == null || s.equals("")) {
return "";
}
return toNotJPY_0(s);
}
/**
*
* @param s
* @return
*/
private static String toNotJPY_0(String s) {
String tmp = "";
String dotTmp = "";
String sysmol = "";
int iSpe = s.indexOf(DOT);
if (iSpe != -1) {
dotTmp = s.substring(iSpe);
if (dotTmp.length() >= 3) {
dotTmp = dotTmp.substring(0, 3);
}
}
if (iSpe > 0)
tmp = s.substring(0, iSpe);
else
tmp = s;
if (tmp.startsWith(MINUS_HYPHEN)) {
tmp = tmp.substring(1);
sysmol = MINUS_HYPHEN;
}
int speMount = 0;
int modValue = 0;
speMount = tmp.length() / 3;
modValue = tmp.length() % 3;
String speResult = "";
if (speMount > 0) {
for (int i = 0; i < speMount + 1; i++) {
if (i == 0) {
speResult = tmp.substring(0, modValue);
if (modValue == 0 && speMount > 1) {
speResult = tmp.substring(0, 3);
tmp = tmp.substring(3, tmp.length());
i++;
} else if (modValue == 0 && speMount == 1) {
speResult = tmp.substring(0, 3);
i++;
} else {
tmp = tmp.substring(modValue, tmp.length());
}
} else {
speResult = speResult + COMMA + tmp.substring(0, 3);// (i-1)*3,
// i*3);
tmp = tmp.substring(3, tmp.length());
}
}
} else {
speResult = tmp;
}
return sysmol + speResult + dotTmp;
}
/**
* 去掉小数后面的0
*
* @param c
* @return
* @author huxiaohai<huxh@bestwiz.cn>
*/
public static String BigdecmalToString(BigDecimal c) {
{
int i;
boolean flag = true;
BigDecimal a = c;
String b = (new Double(a.doubleValue())).toString();
if (b.indexOf(".") != -1) {
i = b.indexOf(".") + 1;
for (; i < b.length(); i++) {
if (b.charAt(i) != '0') {
flag = false;
break;
}
}
}
if (flag == true) {
return a.toBigInteger().toString();
} else {
return String.valueOf(a.doubleValue());
}
}
}
/**
* 获得当前group的保证金
*
* @param groupid
* 组ID
* @param trade
* 保证金数组
* @return
*/
public static String[] checkTrade(String groupName, String[] trade) {
List<String> grouptrade = new ArrayList<String>(0);
int size = trade.length;
for (int i = 0; i < size; i++) {
if (trade[i] != null) {
if (groupName.equals(trade[i].split(",")[0])) {
grouptrade.add(trade[i].split(",")[1]);
}
}
}
return grouptrade.toArray(new String[] {});
}
/**
* init 英字数字の日本語読み方
*
* @return Map<String, String>
*/
public static Map<String, String> ininMap(){
Map<String, String> map = new HashMap<String, String>();
map.put("A", "大字エイ");
map.put("B", "大字ビー");
map.put("C", "大字シー");
map.put("D", "大字ディー");
map.put("E", "大字イー");
map.put("F", "大字エフ");
map.put("G", "大字ジー");
map.put("H", "大字エイチ");
map.put("I", "大字アイ");
map.put("J", "大字ジェイ");
map.put("K", "大字ケイ");
map.put("L", "大字エル");
map.put("M", "大字エム");
map.put("N", "大字エヌ");
map.put("O", "大字オー");
map.put("P", "大字ピー");
map.put("Q", "大字キュー");
map.put("R", "大字アール");
map.put("S", "大字エス");
map.put("T", "大字ティー");
map.put("U", "大字ユー");
map.put("V", "大字ヴイ");
map.put("W", "大字ダブリュー");
map.put("X", "大字エックス");
map.put("Y", "大字ワイ");
map.put("Z", "大字ゼット");
map.put("a", "小字エイ");
map.put("b", "小字ビー");
map.put("c", "小字シー");
map.put("d", "小字ディー");
map.put("e", "小字イー");
map.put("f", "小字エフ");
map.put("g", "小字ジー");
map.put("h", "小字エイチ");
map.put("i", "小字アイ");
map.put("j", "小字ジェイ");
map.put("k", "小字ケイ");
map.put("l", "小字エル");
map.put("m", "小字エム");
map.put("n", "小字エヌ");
map.put("o", "小字オー");
map.put("p", "小字ピー");
map.put("q", "小字キュー");
map.put("r", "小字アール");
map.put("s", "小字エス");
map.put("t", "小字ティー");
map.put("u", "小字ユー");
map.put("v", "小字ヴイ");
map.put("w", "小字ダブリュー");
map.put("x", "小字エックス");
map.put("y", "小字ワイ");
map.put("z", "小字ゼット");
map.put("1", "イチ");
map.put("2", "ニ");
map.put("3", "サン");
map.put("4", "シ");
map.put("5", "ゴ");
map.put("6", "ロク");
map.put("7", "シチ");
map.put("8", "ハチ");
map.put("9", "ク");
map.put("0", "レイ");
return map;
}
/**
* 得到 英字数字の日本語読み方
* @param String words英字数字
* @return String
*/
public static String readWay(String words){
Map<String, String> map = new HashMap<String, String>();
String jpWords="";
map=ininMap();
int wordsAccount=words.length()-1;
for(int i=0;i<words.length();i++){
if(map.get(words.charAt(i)+"")!=null){
if(i==wordsAccount){
jpWords=jpWords+map.get(words.charAt(i)+"");
}else{
jpWords=jpWords+map.get(words.charAt(i)+"")+"・";
}
}
}
jpWords="("+jpWords+")";
map.clear();
return jpWords;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -