美文网首页
Java工作中常用方法备份

Java工作中常用方法备份

作者: 工具人005 | 来源:发表于2018-03-20 08:49 被阅读0次

    在线编程地址:
    http://www.runoob.com/try/runcode.php?filename=HelloWorld&type=java

    import java.util.Arrays;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.HashMap;
    import java.util.Map;
    //http://www.guisd.com/operators.html
    //134 135 136 137 138 139 150 151 152 157 158 159 187 188 147
    //130 131 132 155 156 185 186 145
    //133 153 180 189 
    
    public class HelloWorld {
        public static void main(String []args) {
            String strs = "183 134 135 136 137 138 139 150 151 152 157 158 159 187 188 147 130 131 132 155 156 185 186 145 133 153 180 189";
            String[] str_arr = strs.split(" ");
            Arrays.sort(str_arr);
            System.out.println("排序后:"+ Arrays.asList(str_arr));
    
            Map<String,String> map = new HashMap<>();
            for(int i = 0 ;i<str_arr.length;i++){
                /*判断第二个下标是否存在,不存在的话创建一个默认的*/
                if(map.get(""+str_arr[i].charAt(1))==null){ 
                    map.put(""+str_arr[i].charAt(1),"");
                }
                /*根据每个手机号的第二个标识的第三个值,进行追加*/
                String value = map.get(""+str_arr[i].charAt(1));
                value += str_arr[i].charAt(2);
                map.put(""+str_arr[i].charAt(1),value);
            }
            System.out.println("根据第二位分组后结果:"+ map);
            
            /*生成正则表达试*/
            String compileZ = "^1(";
            boolean isFirst = true;//首个不拼接|
            for(String key : map.keySet()){ 
                compileZ += (isFirst?"":"|") + key + "["+map.get(key)+"]";
                isFirst = false;
            }
            compileZ += ")\\d{8}$";
            System.out.println("得到的正则表达式:"+ compileZ);
                
            String msg = "18311020335";//测试手机号
            Pattern pattern = Pattern.compile(compileZ);
            Matcher matcher = pattern.matcher(msg);
            boolean isMatch = matcher.matches();
            System.out.println(isMatch?"正确":"不正确");
        }
    }
    
    
    /**透明度对照表*/
    public class HelloWorld {
        public static void main(String []args) {
            System.out.println("透明度 | 十六进制");
            System.out.println("------ | ------");
            for (double i = 1; i >= 0; i -= 0.01) {
                i = Math.round(i * 100) / 100.0d;
                int alpha = (int) Math.round(i * 255);
                String hex = Integer.toHexString(alpha).toUpperCase();
                if (hex.length() == 1) hex = "0" + hex;
                int percent = (int) (i * 100);
                System.out.println(String.format("%5d%% | %6s", percent, hex));
            }
        }
    }
    
    /**字体样式定义生成器*/
    public class HelloWorld {
        public static void main(String []args) {
           for(int i =12;i<=48;i=i+2){
               int value = (int)(i*0.9);
               value = value+value%2;
               String show = "<item name=\"font_size_"+i+"\">@dimen/f_px_"+value+"</item>";
               System.out.println(show);
            }
        }
    }
    
    /**10进制转32进制,固定6位,不够前面补0方法*/
    public class HelloWorld {
        public static void main(String []args) {
           for(int i =100000;i<=100050;i++){
               String str = Integer.toString(i,32);
               System.out.println(String.format("%1$6s", str).replaceAll(" ","0"));
            }
        }
    }
    
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    /**找到字符串第一个##号之间的,且不是#号的内容*/
    public class HelloWorld {
        public static void main(String[] args) {
            String msg = "cssffff#aaa#ddf#ddf#fg"; 
            Pattern pattern = Pattern.compile("#[^#]+#");
            Matcher matcher = pattern.matcher(msg);
            while (matcher.find()) {
                System.out.println(matcher.group(0));
                break;//找到一个就不再继续查找
            }
        }
    }
    
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    /**找到url 的?后面的参数*/
    public class HelloWorld {
    public static void main(String[] args) {
    String msg = "[http://www.runoob.com/try/runcode.php?filename=HelloWorld&type=java](http://www.runoob.com/try/runcode.php?filename=HelloWorld&type=java)";
    Pattern pattern = Pattern.compile("\\w+=\\w");
    Matcher matcher = pattern.matcher(msg);
    while (matcher.find()) {
    System.out.println(matcher.group(0));
    //break;//找到一个就不再继续查找
    }
    }
    }
    

    相关文章

      网友评论

          本文标题:Java工作中常用方法备份

          本文链接:https://www.haomeiwen.com/subject/ulcvqftx.html