`
姜中秋
  • 浏览: 86375 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
java字符串替换
public String str_replace2(String con ,String tag,String rep){
    int j=0;
    int i=0;
    int k=0;
    String RETU="";
    String temp =con;
    int tagc =tag.length();
    while(i<con.length()){
      if(con.substring(i).startsWith(tag)){
        temp =con.substring(j,i)+rep;
        RETU+= temp;
        i+=tagc;
        j=i;
      }else{
        i+=1;
      }
    }
    RETU +=con.substring(j);
    return RETU;
  }  
随即数生产器 随机数
package com.test;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

public class TestRandom {

	 /** 
     * 每位允许的字符 
     */  
    private static final String POSSIBLE_CHARS="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";  
      
    /** 
     * 生产一个指定长度的随机字符串 
     * @param length 字符串长度 
     * @return 
     */  
    private String generateRandomString(int length) {  
        StringBuilder sb = new StringBuilder(length);  
        SecureRandom random = new SecureRandom();  
        for (int i = 0; i < length; i++) {  
            sb.append(POSSIBLE_CHARS.charAt(random.nextInt(POSSIBLE_CHARS.length())));  
        }  
        return sb.toString();  
    }  
      
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        Set<String> check = new HashSet<String>();  
        TestRandom obj = new TestRandom();  
          
        //生成2000000个随机字符串,检查是否出现重复  
        for (int i = 0; i < 20; i++) {  
            String s = obj.generateRandomString(16);  
            System.out.println(s);
            if (check.contains(s)) {  
                throw new IllegalStateException("Repeated string found : " + s);  
            } else {  
                if (i % 1000 == 0) System.out.println("generated " + i / 1000 + "000 strings.");  
                check.add(s);  
            }  
        }  
    }  
}
Global site tag (gtag.js) - Google Analytics