Skip to content

Instantly share code, notes, and snippets.

@hygull
Last active December 26, 2016 02:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hygull/b2ff9914c8504882929004776fed3471 to your computer and use it in GitHub Desktop.
Save hygull/b2ff9914c8504882929004776fed3471 to your computer and use it in GitHub Desktop.
Splitting a string into list of words created by hygull - https://repl.it/EmUM/10
/*
Date of creation : 10/12/2016.
Aim of program : To use Fields() to split the string into a list words.
Coded by : Rishikesh Agrawani.
*/
package main
import (
"fmt"
"strings"
)
func main() {
var str string
str = "We should always respect the time" //Only 1 space is there
listOfWords := strings.Fields(str)
fmt.Print(str, ":-\n", listOfWords) //Displaying string and a slice containing all the words
str1 := "A B C D E" //Lets use any no. of spaces
listOfWords = strings.Fields(str1)
fmt.Print("\n\n",str1, ":-\n", listOfWords) //Displaying string and a slice containing all the words
str2 := "Nice @ character is of 8 bits in C" //Only 1 space is there
listOfWords = strings.Fields(str2)
fmt.Print("\n\n",str2, ":-\n", listOfWords) //Displaying string and a slice containing all the words
}
/*.......OUTPUT........................
We should always respect the time:-
[We should always respect the time]
A B C D E:-
[A B C D E]
Nice @ character is of 8 bits in C:-
[Nice @ character is of 8 bits in C]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment