常用的功能的代码片段

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

@Test
    public 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);
    }
 
}

8.二维数组删除指定名称的列:

        /**
         * 
         * @param org  ---数组
         * @param name ---指定要删除列的名称
         * @return 返回要删除列的下标---------- 从0开始, -1表示找不到要删除的列
         */
        public static int getCol(String org[][],String name)      
        {
            for(int i=0;i<org[0].length;i++)
            {
                if(org[0][i].equals(name))
                {
                    return i;
                }
            }
            return -1;
        }
        
        /**
         * 
         * @param org ---数据源
         * @param name-----要删除的列的名称
         * @return 删除列后的二维数组
         */
        public static  String[][] delCol(String org[][],String name)
        {
            String target[][]=new String[org.length][org[0].length-1];
            int lie=getCol(org,name);
            if(lie==-1)
            {
                return org;
            }
            else
            {
                for(;lie<org[0].length-1;lie++)              //删除列,将列右边的数据左移动
                {
                    for(int i=0;i<org.length;i++)
                    {
                        
                            org[i][lie]=org[i][lie+1];   
                    }
                }
            }
            
            for(int k=0;k<org.length;k++)            //对目标数组进行赋值
            {
                
                for(int a=0;a<org[0].length-1;a++)
                {
                    target[k][a]=org[k][a];
                }
            }
            return target;
        }

9.删除二维数组指定行:

        /**
         * 
         * @param org------数据源
         * @param name-------指定行名称
         * @return
         */
        public static int getRow(String org[][],String name)      
        {
            for(int i=0;i<org.length;i++)
            {
                if(org[i][0].equals(name))
                {
                    return i;
                }
            }
            return -1;
        }
 
public static  String[][] delRow(String org[][],String name)
        {
            String target[][]=new String[org.length-1][org[0].length];
            int hang=getRow(org,name);
            if(hang==-1)
            {
                return org;
            }
            else
            {
                for(;hang<org.length-1;hang++)              //删除列,将行下边的数据上移动
                {
                    for(int i=0;i<org[0].length;i++)
                    {
                        
                            org[hang][i]=org[hang+1][i];   
                    }
                }
            }
            
            for(int k=0;k<org.length-1;k++)            //对目标数组进行赋值
            {
                
                for(int a=0;a<org[0].length;a++)
                {
                    target[k][a]=org[k][a];
                }
            }
            return target;
        }
        

Fping操作

根据IP获取地址

根据IP/掩码位获取IP的起始范围、IP总数、IP属不属于某个网段或者打印网段内所有的IP

原文地址:https://www.cnblogs.com/edda/p/13916119.html