1.算法列表
- 拼写inStr
- 去掉字符串后边的0
- 三者最小值
- 计算表达式的值
1.1 拼写inStr
/** * 带逗号的字符串转换成SQL里的inStr("a,b,c"->'a','b','c') * * @param stringWithComma 用逗号分隔的字符串 "a,b,c" * @return inStr可以用在SQL的in语句内 'a','b','c' */ public String getInStr(String stringWithComma) { String splitSymbolComma = ","; ArrayList<String> strList = CollectionUtil.toList(stringWithComma.split(splitSymbolComma)); return strList.stream().collect(Collectors.joining("\',\'", "\'", "\'")); }
1.2 去掉字符串后边的0
/** * 处理字符串后边的0(4100->41) * * @param str 字符串 * @return 不带0的字符串 */ public String dealZero(String str) { String zeroStr = "0"; if (str.endsWith(zeroStr)) { str = str.substring(0, str.length() - 1); return dealZero(str); } else { return str; } }
1.3 三者最小值
/** * 获取三者最小值 * * @param one 值1 * @param two 值2 * @param three 值3 * @return 最小值 */ public int min(int one, int two, int three) { return (one = Math.min(one, two)) < three ? one : three; }
1.4 计算表达式的值
/** * 计算表达式的值(1+1->2) * * @param scoreStr 表达式 * @return 分数 */ private double calculateScoreByStr(String scoreStr) { double score = 0.0; if (StringUtils.isNotBlank(scoreStr)) { ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine se = sem.getEngineByName("js"); try { score = (double) se.eval(scoreStr + "*1.0"); } catch (ScriptException e) { log.error("计算[{}]时出现异常", scoreStr); } } return score; }