字符串去除空格回车以及随机生成中英文字符串

来源:互联网 发布:可视化编程软件 编辑:程序博客网 时间:2024/06/10 16:03

一、字符串去除空格回车以及换行

Pattern p = Pattern.compile("\\s*|\t|\r|\n");

Matcher m = p.matcher(“用于匹配的字符串”);

String dest = "";

dest = m.replaceAll("");  


二、随机生成数字与英文组合字符串

 //生成随机数字和字母,  
    public String getStringRandom(int length) {  
          
        String val = "";  
        Random random = new Random();  
          
        //参数length,表示生成几位随机数  
        for(int i = 0; i < length; i++) {  
              
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";  
            //输出字母还是数字  
            if( "char".equalsIgnoreCase(charOrNum) ) {  
                //输出是大写字母还是小写字母  
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;  
                val += (char)(random.nextInt(26) + temp);  
            } else if( "num".equalsIgnoreCase(charOrNum) ) {  
                val += String.valueOf(random.nextInt(10));  
            }  
        }  
        return val;  
    }


以上为之前开发过程中遇到的问题,经百度之后粘贴的代码。

阅读全文
0 0