检查一个字符串是否以十进制数字开头?

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

Check if a string starts with a decimal digit?

问题

这是以下代码的翻译结果:

var thestr = "192.168.0.1";
if (thestr[0] >= '0' && thestr[0] <= '9'){
    //...
}

这段代码的作用是判断字符串 thestr 的第一个字符是否为数字。如果是数字,则执行相应的操作。

英文:

It looks the following works, is it a good approach?

var thestr = &quot;192.168.0.1&quot;
if (thestr[0]&gt;= &#39;0&#39; &amp;&amp; thestr[0] &lt;= &#39;9&#39;){
    //...
}

答案1

得分: 2

你的解决方案完全没问题。

但是请注意,在Go语言中,字符串被存储为只读的字节切片,其中字节是UTF-8编码的字节序列,索引字符串时索引的是它的字节,而不是字符。但是由于十进制数字('0'..'9')恰好有一个字节,所以在这种情况下测试第一个字节是可以的,但是首先你应该测试len(s) > 0s != ""

以下是一些其他的选择,你可以在Go Playground上尝试它们:

1) 测试字节范围:

这是你的解决方案,可能是最快的一个:

s := "12asdf"
fmt.Println(s[0] >= '0' && s[0] <= '9')

2) 使用fmt.Sscanf()

注意:这种方法也接受以负数开头的字符串作为输入,你需要决定这是否对你有问题(例如,接受"-12asf")。

i := 0
n, err := fmt.Sscanf(s, "%d", &i)
fmt.Println(n > 0, err == nil) // n和err都可以用来进行测试

3) 使用unicode.IsDigit()

fmt.Println(unicode.IsDigit(rune(s[0])))

4) 使用regexp

我可能永远不会使用这种方法,因为它是最慢的,但是这里有一个例子:

r := regexp.MustCompile(`^\d`)
fmt.Println(r.FindString(s) != "")

或者:

r := regexp.MustCompile(`^\d.*`)
fmt.Println(r.MatchString(s))
英文:

Your solution is completely fine.

But note that strings in Go are stored as a read-only byte slice where the bytes are the UTF-8 encoded byte sequence, and indexing a string indexes its bytes, not its runes (characters). But since a decimal digit (&#39;0&#39;..&#39;9&#39;) has exactly one byte, it is ok in this case to test the first byte, but first you should test if len(s) &gt; 0 or s != &quot;&quot;.

Here are some other alternatives, try all on the Go Playground:

1) Testing the byte range:

This is your solution, probably the fastest one:

s := &quot;12asdf&quot;
fmt.Println(s[0] &gt;= &#39;0&#39; &amp;&amp; s[0] &lt;= &#39;9&#39;)

2) Using fmt.Sscanf():

Note: this also accepts if the string starts with a negative number, decide if it is a problem for you or not (e.g. accepts &quot;-12asf&quot;).

i := 0
n, err := fmt.Sscanf(s, &quot;%d&quot;, &amp;i)
fmt.Println(n &gt; 0, err == nil) // Both n and err can be used to test

3) Using unicode.IsDigit():

fmt.Println(unicode.IsDigit(rune(s[0])))

4) Using regexp:

I would probably never use this as this is by far the slowest, but here it is:

r := regexp.MustCompile(`^\d`)
fmt.Println(r.FindString(s) != &quot;&quot;)

Or:

r := regexp.MustCompile(`^\d.*`)
fmt.Println(r.MatchString(s))

答案2

得分: 0

请不要使用正则表达式来完成这个简单的任务 检查一个字符串是否以十进制数字开头?

在这种情况下,我会做以下更改:

  • 在检查第一个符文之前,添加对空字符串的检查
  • 我会将其重新表述为“以数字开头”,因为“数字”这个语义太广泛了。.5e-45是一个数字,但可能不是你想要的。0的语义也不简单:https://math.stackexchange.com/questions/238737/why-do-some-people-state-that-zero-is-not-a-number
英文:

Please do not use regexps for that simple task 检查一个字符串是否以十进制数字开头?

What I would change in this case:

答案3

得分: -1

由于您正在按字符进行比较,并且1到9之间没有字符,我认为您的解决方案是可以的,但它没有考虑后面的其他数字。

例如,如果thestr"192.something.invalid",它就不再是一个IP地址。

我建议使用正则表达式来检查IP地址。

可以使用以下正则表达式:

\b(?:\d{1,3}\.){3}\d{1,3}\b
英文:

Since you are comparing by character and no characters are between 1 and 9, I think your solution is ok, but it does not account for the other numbers following.

For example, if thestr was &quot;192.something.invalid&quot; it's no longer an IP.

I'd recommend using a regular expression to check the IP.

something like

\b(?:\d{1,3}\.){3}\d{1,3}\b

huangapple
  • 本文由 发表于 2015年7月28日 22:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/31678817.html
匿名

发表评论

匿名网友

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

确定