Golang program to splitting into number of substrings


In the Go programming language, a substring is a portion of a string that contains a sequence of characters from the original string. A substring can be obtained in Go by using the slicing syntax on a string value.. strings. Split() can be used in Go (Golang) to divide a string into substrings. The first input to the function is the string to be split, and the second argument is the delimiter that will be used to separate the substrings. A slice of strings containing the substrings is the result of the function. There are other ways to split a string into number of substrings.

Method 1: Using strings.Split() function

In this method, we will split the string into number of substrings using the built-in function strings.split() function whose functioning is described below. Here, this function will divide the string into words and a for loop will iterate over those words to print the substring.

Syntax

func Split(str, sep string) []string

Split() function is used to split a string through a provided separator. This function is present in strings package and it accepts the string to split as an argument along with a separator. The function then returns the final array of strings as a result.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Create a function main and in that function create the string to be divided into a variable called mystr.

  • Step 3 − To divide a string into substrings, use the Split() function, supplying the string and the delimiter as parameters.

  • Step 4 − Use a for loop to iterate through the substrings, then use the fmt.Println() function to output each substring one at a time.

  • Step 5 − It should be noted that this technique assumes the string's delimiter to be a single space character. Change the argument supplied to the strings if you wish to split the string using a different delimiter Split() should be used accordingly.

Example

In the following example use split function.

package main
import (
   "fmt"
   "strings"
)

func main() {
   mystr := "Hello, alexa!"  //create a string
   fmt.Println("The string given here is:", mystr)
   substrings := strings.Split(mystr, " ")  //split the string into substrings using the built-in function split
   fmt.Println("The creation of substrings is reflected here:")
   for _, substring := range substrings {
      fmt.Println(substring)  //run a for loop to print substrings
   }
}

Output

The string given here is: Hello, alexa!
The creation of substrings is reflected here:
Hello,
alexa!

Method 2: Using strings.Index() function

In this method string will split into number of substrings using built-in function strings.Index() function. Using the strings, this program repeatedly utilizes a while loop to determine the index of the delimiter in the string.Index().

Syntax

strings.Index()

Strings can be used in Golang to divide a string into substrings. The first input to the function is the string to be split, and the second argument is the delimiter that will be used to separate the substrings. A slice of strings containing the substrings is the result of the function.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) and strings package in the program where main produces executable Examples and fmt helps in formatting input and output.

  • Step 2 − Create a function main and in that function create the variable mystr with the specified delimiter and the string that has to be split.

  • Step 3 − Set the value of the variable start value to 0.

  • Step 4 − Establish a while loop and by using strings.Index() function, get the index of the delimiter in the substring of str beginning at start value. Break the loop if end value is -1.

  • Step 5 − To the end value, add the start value and using strings, find the substring between start and end.

  • Step 6 − Revise start by adding len and end (delimiter)

  • Step 7 − Until the end is -1, repeat steps 3 through 9.

  • Step 8 − Using start as the beginning point, extract the remaining substring from mystr.

  • Step 9 − Print the substring on the console using fmt.Println() function where ln means new line.

Example

In this example, we are going to use string.index() function

package main
import (
   "fmt"
   "strings"
)

func main() {
   mystr := "Hello, alexa!"  //create a string
   fmt.Println("The string created here is:", mystr)
   fmt.Println("The string after being converted to substring is:")
   delimiter := " "
   start := 0
   for {
      end := strings.Index(mystr[start:], delimiter)
      if end == -1 {
         break
      }
      end += start
      substring := mystr[start:end]
      fmt.Println(substring)
      start = end + len(delimiter) //transform the start value
   } 
   substring := mystr[start:]
   fmt.Println(substring)  //print the substring
}

Output

The string created here is: Hello, alexa!
The string after being converted to substring is:
Hello,
alexa!

Conclusion

We executed the program of splitting string into number of substrings using two different examples. We used built-in functions in both the examples. In the first example we used strings.Split() function and in the second example we used strings.Index() function.

Updated on: 20-Feb-2023

594 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements