生成随机字母,返回为字符串

huangapple go评论55阅读模式
英文:

Java: Generate random letter, return as a String

问题

我需要创建一个方法来生成随机的单个字母,并将其作为String返回,而不是char
我看到像这个的解决方案创建了随机的字符。但我需要我的方法返回String类型。
我设法构建了以下方法:

public static String randomLetter(){
    int num = intInRange(0, 26) + 97;
    char [] temp = new char[1];
    temp[0] = (char) num;
    return new String(temp);
}

基于我们项目中已经存在的方法

public static int intInRange(int min, int max) {
    Random rnd = new Random();
    return rnd.nextInt(max - min) + min;
}

但我不喜欢我目前拥有的,因为我为了达到目标而创建了太多临时变量。
是否有一种更短的方法来获得类似的结果,以避免使用char [] temp

英文:

I need to create a method to geneate random (single) letter and return it as String, not as a char.
I saw solutions like this creating random char. But I need my method to return a String type.
I managed to build the following method:

public static String randomLetter(){
    int num = intInRange(0, 26) + 97;
    char [] temp = new char[1];
    temp[0] = (char) num;
    return new String(temp);
}

Based on already existing method in our project

public static int intInRange(int min, int max) {
    Random rnd = new Random();
    return rnd.nextInt(max - min) + min;
}

But I do not like what I currently have since I'm creating too many temporary variables to achieve the goal.
Is there a way to get similar result in a shorter way to avoid usage of char [] temp ?

答案1

得分: 2

不是这样更有用吗?可以返回任意长度的String,包括1。如果是这样的话,你可以像下面这样做:

public static String randomString(int length) {
    return new Random().
        ints('A', 'z').
        filter(Character::isLetter).
        limit(length).
        collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).
        toString();
}
英文:

Wouldn't it be more useful to be able to return a String of any length, including 1? If so, you could do something like the following:

public static String randomString(int length) {
    return new Random().
        ints('A', 'z').
        filter(Character::isLetter).
        limit(length).
        collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).
        toString();
}

答案2

得分: 1

尝试这样做。首先将返回的整数强制转换为`char`,然后附加`""`以转换为字符串。

public static String randomLetter() {
return (char)(intInRange('a', 'z'+1)) + "";
}

由于`String.valueOf()`首先将`char`包装为`Character`,然后在该对象上调用`String.valueOf()`,我会直接调用`Character.toString()`,避免额外的开销。

public static String randomLetter() {
return Character.toString(intInRange('a', 'z'+1));
}


<details>
<summary>英文:</summary>

Try it like this. First cast the returned int as a `char` and then append `&quot;&quot;` to convert to a String.

public static String randomLetter() {
return (char)(intInRange('a', 'z'+1)) +"";
}

Since `String.valueOf()` first boxes the char to Character and then calls `String.valueOf()` on that object, I would just call `Character.toString()` directly and avoid the extra overhead.

public static String randomLetter() {
return Character.toString(intInRange('a', 'z'+1));
}

huangapple
  • 本文由 发表于 2023年6月5日 19:51:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定