- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Convert String to an Array
In Go Language, we have various data types to store data and the String data type is the one that is used to store a sequence of characters. The size of string variable is 1 or 8 bits. An array on the other hand is a data type that is used to store data at contiguous memory locations.
Syntax
func fields(s string) []string
fields() function is present in strings package and is used to convert a string to an array of string. This function takes a string value as an argument and returns the corresponding array as the result.
func split(s, sep string) []string
The function split() takes two strings as an argument on e is the string that we wish to split and another is the character or alphabets through which we wish to split the string s. The function returns the final output as an array containing the strings obtained after splitting.
func typeofobject(x interface{})
The typeof() function is used to get the type of any variable. This function is present in reflect package and it takes the variable whose type is to be determined as an argument. The function then returns the type of the variable specified as the result.
Method 1: Using Fileds() function
In this method we are going to use an inbuild function called as Fields().
Algorithm
Step 1 − First, we need to define the package main.
Step 2 − Then import the package fmt and strings.
Step 3 − Calling the main() function.
Step 4 − Initializing and defining a string named s and assign value to it. Further, print this string on the screen
Step 5 − Now call the fields() function defined in the strings package and pass the above initialized string as argument to it. The function returns the result by performing the conversion.
Step 6 − Store the array returned by the function in a separate variable and print the result on the screen using fmt.Println() function.
Example
In this example we will use the fields() function to convert string to array.
package main import ( "fmt" "reflect" "strings" ) func main() { // initializing the string variable and assign value to it var s string = "this is a sentence lets break it !" fmt.Println("The given data is:\n", s, "and its type is", reflect.TypeOf(s)) arrayOfString := strings.Fields(s) fmt.Println() fmt.Println("The array obtained from the string is:\n", arrayOfString) }
Output
The given data is: this is a sentence lets break it ! and its type is string The array obtained from the string is: [this is a sentence lets break it !]
Method 2: Using Split() Function
In this method, we will use split() function to convert array to string.
Algorithm
Step 1 − First, we need to define the package main and import the package fmt and strings.
Step 2 − Next, call the main() function.
Step 3: − Initialize and define a string named s and assign value to it that we wish to split. Initializing an empty string variable named sep and assign it with a value.
Step 4 − Now, print the string on the screen using fmt.Println() function.
Step 5 − This will be the value through which s string will get separated. Store “ ” a space in the sep variable as we wish to obtain every word of the string but in array format.
Step 6 − Now call the split() function defined in the strings package and pass the string as well as sep variable as argument to it. The function will split the string according to provided variable and return the corresponding array.
Step 7 − Store the array of strings returned by the function in a separate variable and print the result on the screen using fmt.Println() function.
Example
Following is the golang program to convert a string to an array using split() function.
package main import ( "fmt" "reflect" "strings" ) func main() { // initializing the string variable var s string = "this is a sentence lets break it !" var sep string = " " fmt.Println("The given data is:\n", s, "and its data type is:", reflect.TypeOf(s)) arrayOfString := strings.Split(s, sep) fmt.Println() fmt.Println("The array obtained after splitting the string is:\n", arrayOfString) }
Output
The given data is: this is a sentence lets break it ! and its data type is: string The array obtained after splitting the string is: [this is a sentence lets break it !]
Method 3: Using FOR Loop
In this method, we will use a for loop to convert a string to an array.
Algorithm
Step 1 − First, we need to define the package main.
Step 2 − Then import the package fmt and reflect.
Step 3 − Calling the main() function.
Step 4 − Initializing and defining a string named s and assign value to it. Further, print this string on the screen
Step 5 − Now use the for loop to iterate over the array and append each element of the string to a new array.
Step 6 − At last print the result on the screen using fmt.Println() function.
Example
Following is a go language program to convert a string to an array using for loop
package main import ( "fmt" "reflect" ) func main() { str := "hello world" fmt.Println("The given data is:\n", str, "and its data type is:", reflect.TypeOf(str)) arr := make([]string, len(str)) for i, r := range str { arr[i] = string(r) } fmt.Println() fmt.Println("The array obtained by the above string is:",arr) }
Output
The given data is: hello world and its data type is: string The array obtained by the above string is: [h e l l o w o r l d]
Conclusion
We have successfully compiled and executed a go language program to convert a string to an array along with examples. We have used fields() and split() predefined functions defined in strings library for this task. The functions accept the strings as argument and return the corresponding array of strings after performing the task.
- Related Articles
- Golang Program To Convert Set Of String To Array Of String
- Golang Program to Convert Linked list to an Array
- Haskell Program to Convert String to an Array
- Swift program to convert string to an array
- Golang Program to Convert String to Object
- Golang Program to Convert Character to String
- Golang Program to convert Boolean to String
- Golang Program To Convert Array To Set
- Golang Program to convert string variables to double
- Golang Program To Convert An Array Into A String And Join Elements With A Specified Character
- Golang program to convert file to byte array
- Golang program to convert array into slice
- Golang program to convert slice into array
- Write a program to convert an array to String in Java?
- Golang Program to Convert String Value to Byte Value
