确保每个字母都在字符串中 (Kotlin)

发布于 2025-01-16 12:57:43 字数 478 浏览 1 评论 0原文

所以我有一个问题,我要检查一个字符串中是否包含字母表中的每个字母。我能够检查字符串中是否有字母表,但我不确定如何检查所述字符串中是否存在每个字母表。这是代码

fun isPangram (pangram: Array<String>) : String {
    var panString : String
    var outcome = ""

    for (i in pangram.indices){
        panString = pangram[i]

        if (panString.matches(".^*[a-z].*".toRegex())){
            outcome = outcome.plus('1')
        }
        else {outcome = outcome.plus('0')}

    }
    return outcome

    }

欢迎任何想法谢谢。

So I have a question where I am checking if a string has every letter of the alphabet in it. I was able to check if there is alphabet in the string, but I'm not sure how to check if there is EVERY alphabet in said string. Here's the code

fun isPangram (pangram: Array<String>) : String {
    var panString : String
    var outcome = ""

    for (i in pangram.indices){
        panString = pangram[i]

        if (panString.matches(".^*[a-z].*".toRegex())){
            outcome = outcome.plus('1')
        }
        else {outcome = outcome.plus('0')}

    }
    return outcome

    }

Any ideas are welcomed Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苯莒 2025-01-23 12:57:43

我认为检查字母表范围的所有成员是否都在每个字符串中比使用正则表达式更容易:

fun isPangram(pangram: Array<String>): String =
    pangram.joinToString("") { inputString ->
        when {
            ('a'..'z').all { it in inputString.lowercase() } -> "1"
            else -> "0"
        }
    }

I think it would be easier to check if all members of the alphabet range are in each string than to use Regex:

fun isPangram(pangram: Array<String>): String =
    pangram.joinToString("") { inputString ->
        when {
            ('a'..'z').all { it in inputString.lowercase() } -> "1"
            else -> "0"
        }
    }
樱花细雨 2025-01-23 12:57:43

嗨,这是如何使用正则表达式

Kotlin 语法

fun isStrinfContainsAllAlphabeta( input: String) {
return input.lowercase()
  .replace("[^a-z]".toRegex(), "")
  .replace("(.)(?=.*\\1)".toRegex(), "")
  .length == 26;
}

在 java 中:

public static boolean  isStrinfContainsAllAlphabeta(String input) {
  return input.toLowerCase()
 .replace("[^a-z]", "")
 .replace("(.)(?=.*\\1)", "")
 .length() == 26;
}

该函数仅接受一个字符串。第一个“replaceAll”删除所有非字母字符,第二个“replaceAll”删除重复的字符,然后检查剩余的字符数。

Hi this is how you can make with regular expression

Kotlin Syntax

fun isStrinfContainsAllAlphabeta( input: String) {
return input.lowercase()
  .replace("[^a-z]".toRegex(), "")
  .replace("(.)(?=.*\\1)".toRegex(), "")
  .length == 26;
}

In java:

public static boolean  isStrinfContainsAllAlphabeta(String input) {
  return input.toLowerCase()
 .replace("[^a-z]", "")
 .replace("(.)(?=.*\\1)", "")
 .length() == 26;
}

the function takes only one string. The first "replaceAll" removes all the non-alphabet characters, The second one removes the duplicated character, then you check how many characters remained.

我不吻晚风 2025-01-23 12:57:43

只是为了反弹 Tenfour04 的解决方案,如果您编写两个函数(一个用于全语法检查,一个用于处理数组),我觉得您可以使其更具可读性,因为它们实际上是两个单独的任务。 (这在一定程度上是向您展示一些 Kotlin 技巧的借口!)

val String.isPangram get() = ('a'..'z').all { this.contains(it, ignoreCase = true) }

fun checkPangrams(strings: Array<String>) =
    strings.joinToString("") { if (it.isPangram) "1" else "0" }

您可以使用 扩展函数 而不是 扩展属性 (所以it.isPangram()),或者只是一个带参数的普通函数 (isPangram(it)),但如果您愿意,您可以编写几乎读起来像英语的内容!

Just to bounce off Tenfour04's solution, if you write two functions (one for the pangram check, one for processing the array) I feel like you can make it a little more readable, since they're really two separate tasks. (This is partly an excuse to show you some Kotlin tricks!)

val String.isPangram get() = ('a'..'z').all { this.contains(it, ignoreCase = true) }

fun checkPangrams(strings: Array<String>) =
    strings.joinToString("") { if (it.isPangram) "1" else "0" }

You could use an extension function instead of an extension property (so it.isPangram()), or just a plain function with a parameter (isPangram(it)), but you can write stuff that almost reads like English, if you want!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文