积累一些常用的功能的代码片段。持续更新

来源:互联网 发布:唯一网络王宇杰简介 编辑:程序博客网 时间:2024/04/30 18:00

1.获取字符串中某个字符段第几次出现的位置:例如拿url中第三个‘/’的位置,并把该位置(包含该位置)后的字符串输出:

@Testpublic void tetssss(){System.out.print(testss("http://localhost:8080/ylitsm/androidpublic/alogin.do?","/",3));}/** *  *author:cwy *说明: *参数: * @param string----------字符串 * @param sub------------子字符串 * @param index----------第几个(从1开始) * @return */public  String testss(String string,String sub,int index){ //这里是获取"/"符号的位置    Matcher slashMatcher = Pattern.compile(sub).matcher(string);    int mIdx = 0;    while(slashMatcher.find()) {       mIdx++;       //当"/"符号第三次出现的位置       if(mIdx == index){          break;       }    }  return string.substring(slashMatcher.start())+"";}


2.根据两个ip,开始ip,结束ip,算出他们中间存在的ip

/**  * @author cwy  * @date 2017-1-4 下午8:54:22 * @version 版本号码 * @TODO 描述:根据IP起始地址和结尾地址计算两者之间的地址 * ipfrom---------开始ip * ipto-----------结束ip */public static Object[] GET_IP_ARR(String ipfrom, String ipto) {    List<String> ips = new ArrayList<String>();    String[] ipfromd = ipfrom.split("\\.");    String[] iptod = ipto.split("\\.");    int[] int_ipf = new int[4];    int[] int_ipt = new int[4];    for (int i = 0; i < 4; i++) {        int_ipf[i] = Integer.parseInt(ipfromd[i]);        int_ipt[i] = Integer.parseInt(iptod[i]);    }    for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {        for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1]                : 255); B++) {            for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2]                    : 255); C++) {                for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3]                        : 255); D++) {                    ips.add(new String(A + "." + B + "." + C + "." + D));                }            }        }    }    return ips.toArray();}    public static void main(String[] args) {    Object[] str=IpUtil.GET_IP_ARR("192.168.1.1","192.168.1.255");    for(int i=0;i<str.length;i++)    {    System.out.println(str[i].toString());    }    }


3.从数据库拿数据时,将带下划线的字段去掉,并把下划线后的首字母变成大写。

commons.lang3包很好用

1.先全部变成小写字符,2,进行变化

String property = convertProperty(StringUtils.lowerCase(columnLabel)); // 属性名称(小写);/** * 格式数据库字段,把下划线去掉并紧跟首字母大写,转换为对象属性 *  * @param srcStr * @return */private static String convertProperty(String srcStr) {String property = "";if (srcStr.indexOf("_") > -1) { // 存在下划线StringBuilder sb = new StringBuilder();String[] strs = srcStr.split("_");sb.append(strs[0]);for (int i = 1; i < strs.length; i++) {sb.append(StringUtils.capitalize(strs[i])); // 首字母大写}property = sb.toString();} else {property = srcStr;}return property;}

4.生成首字母大写的set,或get方法:

/** * 首字母大写并生成setXX方法 *  * @param srcStr * @return */private static String setFirstCharacterToUpper(String srcStr) {String setMethod = "set" + StringUtils.capitalize(srcStr); // 首字母大写return setMethod;}

5.判断请求来自手机还是网页

/**  * @author cwy  * @date 2017-1-13 上午9:36:54 * @version 版本号码 * @TODO 描述:判断设备属于手机还是网页 */public static boolean  isMobileDevice(String requestHeader){/** * android : 所有android设备 * mac os : iphone ipad * windows phone:Nokia等windows系统的手机 */String[] deviceArray = new String[]{"android","mac os","windows phone"};if(requestHeader == null)return false;requestHeader = requestHeader.toLowerCase();for(int i=0;i<deviceArray.length;i++){if(requestHeader.indexOf(deviceArray[i])>0){return true;}}return false;}


6.按位数生成随机数字,在生成工单的时候很好用

/** * 按位数生成字符串 *  * @param charCount *            位数 * @return */public static String getRandNum(int charCount) {String charValue = "";for (int i = 0; i < charCount; i++) {char c = (char) (randomInt() + '0');charValue += String.valueOf(c);}return charValue;}/** * 随机生成0~9之间数字 */private static int randomInt() {Random r = new Random();int num = r.nextInt(10); // 随机生成0~9之间数字return num;}

7.读取Properties文件工具类:

public class PropertiesUtil {private static Logger logger = LogManager.getLogger(PropertiesUtil.class);private static Properties prop = null;private PropertiesUtil() {}static {prop = new Properties();InputStream in = PropertiesUtil.class.getResourceAsStream("/properties/Prop.properties"); // 从根目录下读文件try {prop.load(in);} catch (IOException e) {logger.debug("初始化properties文件错误......");e.printStackTrace();}}/** * 获取值 *  * @param key * @return */public static String get(String key) {String valString = prop.getProperty(key, "");//logger.debug("key:" + key + "      value:" + valString);return valString;}/** *  * 在内存中修改key对应的value值 */public static void setProper(String key, String value) {prop.setProperty(key, value);}}








0 0
原创粉丝点击