mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 15:18:01 +00:00
Add Parse A String Into Individual Fields as a Go TIL
This commit is contained in:
39
go/parse-a-string-into-individual-fields.md
Normal file
39
go/parse-a-string-into-individual-fields.md
Normal file
@@ -0,0 +1,39 @@
|
||||
# Parse A String Into Individual Fields
|
||||
|
||||
Let's say you're reading in data from a file or otherwise dealing with an
|
||||
arbitrary string of data. If that string has a series of values separated by
|
||||
whitespace, you can parse it into individual fields with
|
||||
[`strings.Fields`](https://pkg.go.dev/strings#Fields).
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data := "3 5 2 6 7 1 9"
|
||||
fields := strings.Fields(data)
|
||||
|
||||
fmt.Printf("Fields: %v", fields)
|
||||
// [3 5 2 6 7 1 9]
|
||||
}
|
||||
```
|
||||
|
||||
Here is another example where we can see that `strings.Fields` deals with
|
||||
multiple whitespace and surrounding whitespace:
|
||||
|
||||
```go
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data := " go java c++ rust "
|
||||
fields := strings.Fields(data)
|
||||
|
||||
fmt.Printf("%v", fields)
|
||||
// [go java c++ rust]
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user