java常用的语句

//判断一个长的字符串中是否包含某一个短的字符串
if (str1.indexOf(str2) != -1) {
    return true;//存在
}else {
    return false;
}
/**
 * 消息模板关键字替换
 * @param template  消息内容
 * @param map 替换key-value
 * @return
 */
public static String replaceTemplate(String template, Map<String, Object> map){
    if(!StringUtils.isBlank(template)){
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            template = template.replaceAll(entry.getKey(), entry.getValue().toString());
        }
    }
    return template;
}
/**
 * 生产的随机串
 * @param num 长度
 * @return
 */
public static String genRadomNbr(Integer num) {
    Random random = new Random();
    String result = "";
    for (int i = 0; i < num; i++) {
        result += random.nextInt(10);
    }
    return result;
}
/**
 * 组装返回的json数据
 * @param data
 * @param code
 * @param message
 * @return
 */
public JSONObject assemblyJson(Object data, String code, String message) {
    JSONObject jsonObject = new JSONObject();// 创建对象
    jsonObject.put("data", data);// 数据不为空的时候设置返回数据
    jsonObject.put("message", message);// 设置状态数据
    jsonObject.put("code", code);
    return jsonObject;

}
//删除某一字符
public static String deleteStr(String str, char delChar){
    String delStr = "";
    for (int i = 0; i < str.length(); i++) {
        if(str.charAt(i) != delChar){
            delStr += str.charAt(i);
        }
    }
    return delStr;
}

 

posted @ 2019-07-19 13:47  An-Optimistic-Person  阅读(312)  评论(0编辑  收藏  举报