检查一个字符是否为俄语字符。

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

Check if a character is Russian

问题

我想知道一个字符串是否包含俄语/西里尔字母。

对于拉丁字符,我会这样做(伪代码):

text := "test"
for _, r := range []rune(text) {
    if r >= 'a' && r <= 'z' {
        return True
    }
}
return False

那么对于俄语/西里尔字母,应该如何实现呢?

英文:

I would like to know if a string contains Russian/Cyrillic characters.

For latin characters, I do something like this (pseudocode):

text := &quot;test&quot;
for _, r := range []rune(text) {
	if r &gt;= &#39;a&#39; &amp;&amp; r &lt;= &#39;z&#39; {
        return True
    }
}
return False

What is the corresponding way to do it for Russian/Cyrillic alphabet?

答案1

得分: 14

这似乎有效:

unicode.Is(unicode.Cyrillic, r) // r 是一个 rune
英文:

This seems to work

unicode.Is(unicode.Cyrillic, r) // r is a rune

答案2

得分: 1

我已经根据这个Unicode表为查找俄语大写字符编写了一个示例实现:

func isRussianUpper(text string) bool {
    for _, r := range []rune(text) {
        if r < '\u0410' || r > '\u042F' {
            return false
        }
    }
    return true
}

你可以按照这种方式处理任何字符集。只需修改你感兴趣的字符的代码即可。

英文:

I went on and did this example implementation for finding russian uppercase chars, based on this Unicode chart:

func isRussianUpper(text string) bool {
	for _, r := range []rune(text) {
		if r &lt; &#39;\u0410&#39; || r &gt; &#39;\u042F&#39; {
			return false
		}
	}
	return true
}

You can do any set of characters this way. Just modify the codes of characters you are interested in.

huangapple
  • 本文由 发表于 2017年6月28日 04:30:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/44789336.html
匿名

发表评论

匿名网友

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

确定