英文:
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 = "192.168.0.1"
if (thestr[0]>= '0' && thestr[0] <= '9'){
//...
}
答案1
得分: 2
你的解决方案完全没问题。
但是请注意,在Go语言中,字符串被存储为只读的字节切片,其中字节是UTF-8编码的字节序列,索引字符串时索引的是它的字节,而不是字符。但是由于十进制数字('0'..'9')恰好有一个字节,所以在这种情况下测试第一个字节是可以的,但是首先你应该测试len(s) > 0
或s != ""
。
以下是一些其他的选择,你可以在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 ('0'..'9'
) has exactly one byte, it is ok in this case to test the first byte
, but first you should test if len(s) > 0
or s != ""
.
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 := "12asdf"
fmt.Println(s[0] >= '0' && s[0] <= '9')
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 "-12asf"
).
i := 0
n, err := fmt.Sscanf(s, "%d", &i)
fmt.Println(n > 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) != "")
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:
- add check for empty string before checking for the first rune
- I would rephrase it as "starts with a digit" as the number semantic is too broad.
.5e-45
is a number, but probably it is not what you want. 0's semantic is also not simple: https://math.stackexchange.com/questions/238737/why-do-some-people-state-that-zero-is-not-a-number
答案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 "192.something.invalid"
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论