简体   繁体   English

生成由az和0-9组成的1-5个字符长的字符串

[英]Generate one to five character length string consisting of a-z and 0-9

I'd like to generate all possible strings, from length 1 to length 5, consisting of the characters az and 0-9. 我想生成所有可能的字符串,从长度1到长度5,由字符az和0-9组成。

I've found snippets of code that can probably help me, but I am stuck on modifying them to achieve what I need. 我发现了可能可以帮助我的代码片段,但是我一直坚持修改它们以实现所需。

This bit of code I found on SO will produce all possible string combinations of length 4 using az and 0-9, 1679616 strings. 我在SO上找到的这段代码将使用az和0-9、1679616字符串生成长度为4的所有可能字符串组合。

public static void generate(){
    int MAX = 36;
    long count = 1L * MAX * MAX * MAX * MAX;

    int counter = 0;
    char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
    int[] digits = new int[4];
    char[] output = "0000".toCharArray();

        for (int i= 0; i < count; i++){
            String name = String.valueOf(output);

            for(int d =  3; d >=0; --d){
                digits[d] = (digits[d] + 1) % MAX;
                output[d] = alphabet[digits[d]];
                if (digits[d] > 0){
                    break;
                }
            }
            counter++;
            System.out.println(name);
        }
    System.out.println(counter);
}

Now that's not what I need exactly, but it's close and maybe I can make it work! 现在,这不是我真正需要的,但是已经很接近了,也许我可以使它工作! I've tried modifying in it the following way but there are index issues that are throwing ArrayOutBounds exceptions everywhere. 我曾尝试通过以下方式对其进行修改,但是索引问题到处都引发ArrayOutBounds异常。 The thought process is, I can just wrap this entire block in another for loop that deals with the size of digits and output, which would go from 1 to 5. The more time I spend modifying, the uglier it gets and the more I am thinking, "there must be a cleaner way". 考虑的过程是,我可以将整个块包装在另一个for循环中,该循环处理数字和输出的大小,从1到5。我花的修改时间越多,获取的丑陋程度就越大。思考,“必须有一种更清洁的方式”。

public static void generate(){
    int MAX = 36;
    long count = 1L * MAX * MAX * MAX * MAX * MAX;

    String build = null;
    int counter = 0;
    char[] alphabet = "0123456789abcdefghijklmnopqrstuvwxyz".toCharArray();
    int[] digits = null;
    char[] output = null;

    for (int j = 1; j < 5; j++){
      build = null; //clear build
      digits = null; //clear digits
      outputs = null; //clear outputs
      digits = new int[j]; //initialize digits to size j

      //small loop to append "0" to build 
      for (int m = 0; m < j; m++){
          build = build + "0";
      }
      output = build.toCharArray(); //convert our string to charArray

        for (int i= 0; i < count; i++){
            String name = String.valueOf(output);

            for(int d =  3; d >=0; --d){
                digits[d] = (digits[d] + 1) % MAX;
                output[d] = alphabet[digits[d]];
                if (digits[d] > 0){
                    break;
                }
            }
            counter++;
            System.out.println(name);
        }
    }
    System.out.println(counter);
}

You need to modify the lines 您需要修改行

    long count = 1L * MAX * MAX * MAX * MAX * MAX; // one more * MAX

    ...

    int[] digits = new int[5]; // from [4]
    char[] output = "00000".toCharArray(); // from 0000

    ...

            for(int d =  4; d >=0; --d){ // from d = 3

More generally, if n is the number of digits, 更一般而言,如果n是位数,

    long count = 1L;
    for( int i=0; i<n; i++ ) count *= MAX;

    ...

    int[] digits = new int[n];
    char[] output = new char[n];
    Arrays.fill( output, '0' );

    ...

            for(int d = n-1; d >=0; --d){

You could modify generate to take a leading character as a parameter and print all 5-character strings that start with that character. 您可以修改generate以将前导字符作为参数,并打印以该字符开头的所有5个字符的字符串。 Then call this modified generate with each of the possible leading characters. 然后使用每个可能的前导字符调用此修改后的generate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Regex az,AZ,0-9和(。)(_)( - ) - Java Regex a-z, A-Z, 0-9 and (.)(_)(-) 替换字符串az 0-9和旁边的所有字符, - Replace all characters in string beside a-z 0-9 and , 如何制作一个正则表达式来匹配以 0-9 或 az 开头的带有重音符号的字符串,并且必须只接受这个特殊字符 - 单词之间的 _ '? - How to make a regex to match string that starts with 0-9 or a-z with accents and must accept only this special character - _ ' between words? 拒绝包含az,AZ,0-9以外字符的字符串 - Reject a string that contains characters other than a-z, A-Z, 0-9 包含除 [AZ][az][0-9][\\s][-] 以外的任何字符串的正则表达式 - Regex for string containing anything other than [A-Z][a-z][0-9][\s][-] REGEX az 0-9但不仅仅是数字 - REGEX a-z 0-9 but not only numbers 如何生成 0-9 之间的数字或 AZ 之间的字母,然后将其初始化为变量 - How do i generate a number between 0-9 or letter between A-Z and then initialize it as a variable 如何从给定的字符串中获取字符数组(Az,无数字)? - How to get an array of character (A-z, no numbers) from a given string? 从字符串中删除除数组中az之外的所有字符 - removing all character from string except a-z in array java中的Char Tester(大写AZ,小写az,数字0-9,其他) - Char Tester in java (uppercase A-Z,lowercase a-z,digit 0-9,other)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM