strings.Split表现奇怪

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

strings.Split acting weird

问题

我正在对日期进行简单的字符串拆分操作。

日期的格式是 2015-10-04

  1. month := strings.Split(date, "-")

输出结果是 [2015 10 03]

如果我使用 month[0],它会返回 2015,但是当我使用 month[1] 时,它会返回

  1. panic: runtime error: index out of range

尽管明显不是这样。我使用方法有问题吗?你知道发生了什么吗?

英文:

I am doing a simple strings.Split on a date.

The format is 2015-10-04

  1. month := strings.Split(date, "-")

output is [2015 10 03].

If I do month[0] it returns 2015 but when I do month1, it returns

  1. panic: runtime error: index out of range

Though it clearly isn't. Am I using it wrong? Any idea what is going on?

答案1

得分: 7

这是一个完整的工作示例:

  1. package main
  2. import "strings"
  3. func main() {
  4. date := "2015-01-02"
  5. month := strings.Split(date, "-")
  6. println(month[0])
  7. println(month[1])
  8. println(month[2])
  9. }

输出结果:

  1. 2015
  2. 01
  3. 02

Playground


也许你没有使用正确的 "破折号" 字符?有很多种

  1. +-------+--------+----------+
  2. | 符号 | 代码 | 实体编码 |
  3. +-------+--------+----------+
  4. | - | U+002D | - |
  5. | ֊ | U+058A | ֊ |
  6. | ־ | U+05BE | ־ |
  7. | | U+1806 | ᠆ |
  8. | | U+2010 | ‐ |
  9. | | U+2011 | ‑ |
  10. | | U+2012 | ‒ |
  11. | | U+2013 | – |
  12. | | U+2014 | — |
  13. | | U+2015 | ― |
  14. | | U+207B | ⁻ |
  15. | | U+208B | ₋ |
  16. | | U+2212 | − |
  17. | | U+FE58 | ﹘ |
  18. | | U+FE63 | ﹣ |
  19. | | U+FF0D | - |
  20. +-------+--------+----------+

以下是使用不同输入字符串的代码,也会引发索引越界异常:

  1. package main
  2. import "strings"
  3. func main() {
  4. date := "2015‐01‐02" // 使用 U+2010 破折号
  5. month := strings.Split(date, "-")
  6. println(month[0])
  7. println(month[1])
  8. println(month[2])
  9. }

Playground.

英文:

Here's a complete working example:

  1. package main
  2. import "strings"
  3. func main() {
  4. date := "2015-01-02"
  5. month := strings.Split(date, "-")
  6. println(month[0])
  7. println(month[1])
  8. println(month[2])
  9. }

Output:

  1. 2015
  2. 01
  3. 02

Playground


Perhaps you're not using the correct "dash" character? There are lots:

  1. +-------+--------+----------+
  2. | glyph | codes |
  3. +-------+--------+----------+
  4. | - | U+002D | - |
  5. | ֊ | U+058A | ֊ |
  6. | ־ | U+05BE | ־ |
  7. | | U+1806 | ᠆ |
  8. | | U+2010 | ‐ |
  9. | | U+2011 | ‑ |
  10. | | U+2012 | ‒ |
  11. | | U+2013 | – |
  12. | | U+2014 | — |
  13. | | U+2015 | ― |
  14. | | U+207B | ⁻ |
  15. | | U+208B | ₋ |
  16. | | U+2212 | − |
  17. | | U+FE58 | ﹘ |
  18. | | U+FE63 | ﹣ |
  19. | | U+FF0D | - |
  20. +-------+--------+----------+

Here is the code with a different input string, which also throws an index out of bounds exception:

package main

  1. import "strings"
  2. func main() {
  3. date := "20150102" // U+2010 dashes
  4. month := strings.Split(date, "-")
  5. println(month[0])
  6. println(month[1])
  7. println(month[2])
  8. }

Playground.

huangapple
  • 本文由 发表于 2016年3月9日 06:11:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/35879039.html
匿名

发表评论

匿名网友

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

确定