随机生成指定长度的全半角日文假名

阅读更多

package cn.com.oneslife;

import java.util.Random;

public class RandomUtil {

    static {
        StringBuilder sb = new StringBuilder();
        for(char c = (char)0x3041; c < (char)0x3094; c++){
            sb.append(c);
        }
        JP_SBC_HIRAGAN = sb.toString();
        
        sb = new StringBuilder();
        for(char c = (char)0x30A1; c < (char)0x30F6; c++){
           sb.append(c);
        }
        JP_SBC_KATAKANA = sb.toString();
        
        sb = new StringBuilder();
        for(char c = (char)0xFF66; c < (char)0xFF9F; c++){
           sb.append(c);
        }
        JP_KATAKANA = sb.toString();
        random = new Random();
    }

    public final static int SBC_HIRAGAN = 0;
    public final static int SBC_KATAKANA = 1;
    public final static int KATAKANA = 2;
    
    public final static String markerString(int length, int type){
        char[] c = new char[length];
        for(int index = 0; index < length; index++){
            switch (type) {
            case SBC_HIRAGAN:
                c[index] = JP_SBC_HIRAGAN.charAt(random.nextInt(length));
                break;
            case SBC_KATAKANA:
                c[index] = JP_SBC_KATAKANA.charAt(random.nextInt(length));
                break;
            default:
                c[index] = JP_KATAKANA.charAt(random.nextInt(length));
                break;
            }
        }
        return new String(c);
    }
    
    public static void main(String[] args) {
        System.out.println(markerString(20,SBC_HIRAGAN));
        System.out.println(markerString(20,SBC_KATAKANA));
        System.out.println(markerString(20,KATAKANA));
    }
    
    private final static String JP_SBC_HIRAGAN;
    private final static String JP_SBC_KATAKANA;
    private final static String JP_KATAKANA;
    private final static Random random;
}
 

你可能感兴趣的:(C,C++,C#)