Golang Program to get a substring from the string


A substring in Go is a portion of a larger string. It is specified by providing a start index and a length, and it contains the characters of the original string starting at the start index and going up to the specified length. The characters in a substring are still a part of the original string and share the same memory.

In Go, a string is a sequence of characters. It is an immutable data type, meaning that once a string is created, it cannot be modified. Strings are enclosed in double quotes ("") and can contain any combination of letters, numbers, and symbols.

Method 1: By Using Trimspace Function

In this example we will write a go language program to get the substring from the string by using an external function. The function we create will take the string as an argument along with the start and end variables between which the string elements should be obtained.

Syntax

For Output Code pre class

The trimSpace() function is present in strings package and is used to remove the spaces between the characters of a string. The function accept the required string as an argument and returns the final string obtained after removing the spaces between the words of it.

Algorithm

  • Step 1 − First, we need to import the fmt and strings package.

  • Step 2 − Create a function that takes the string along with it’s the two indexes within which the substring is to be obtained as arguments and returns the substring.

  • Step 3 − Start the main() function.

  • Step 4 − Inside the main initialize a string variable and store value in it.

  • Step 5 − print the variable on the screen.

  • Step 6 − Now call the sub string function by passing the required arguments to it and store the result returned by the function in a variable.

  • Step 7 − The variable contains the sub string between the given indices. Print them on the screen.

Example

Go language program to get a substring from the string by using an external function

package main
import (
   "fmt"
   "strings"
)

// Substring function to get a substring from the string
func Substring(str string, start, end int) string {
   return strings.TrimSpace(str[start:end])
}
func main() {
   // initializing a string
   var str string = "Live young live free"
   fmt.Println("The given string is:", str)

   // Get substring from index 5 to 15
   substring := Substring(str, 5, 15)

   fmt.Println("The string values between the index 5 and 15 are:", substring)
}

Output

The given string is: Live young live free
The string values between the index 5 and 15 are: young live

Method 2: Using Concept Of Indexing

In this article we will use the concept of indexing in order to write a go language program to get a substring from the original string.

Algorithm

  • Step 1 − First, we need to import the fmt package.

  • Step 2 − Start the main() function.

  • Step 3 − Inside the main() initialize a string variable and assign value to it

  • Step 4 − print this array on the screen by using fmt.Println() function.

  • Step 5 − Further, initialize two integer type variables to store the starting and ending index of the array.

  • Step 6 − Store the result obtained in a variable called result and print this variable on the screen by using fmt.Println() function.

Example

Go language program to get a substring from the string by using concept of indexing

package main
import "fmt"
func main() {

   // initializing a string
   var str string = "Prevention is better than cure"
   fmt.Println("The given array is:", str)

   var startInd int = 11
   var endInd int = 20

   result := str[startInd:endInd]
   fmt.Println()
   fmt.Println("The substring is required between", startInd, "and", endInd)

   fmt.Println("Array obtained after getting elements between the above indices are:", result)
}

Output

The given array is: Prevention is better than cure
The substring is required between 11 and 20
Array obtained after getting elements between the above indices are: is better

Method 3: By Using Split Function

In this example we will use the split function in order to get a substring from a string in go language.

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 − First, we need to import the fmt and strings packages.

  • Step 2 − Then start the main() function.

  • Step 3 − Inside this function initialize a string variable and assign value to it.

  • Step 4 − Further print the string on the screen.

  • Step 5 − Use the split() function present in strings package

  • Step 6 − Store Index the element from the substring in a new variable.

  • Step 7 − Now, print the substring on the screen by using fmt.Println() function.

Example

Go langauage program to get a substring from the string by using split function

package main
import (
   "fmt"
   "strings"
)

func main() {
   // Initializing a string
   var str string = "Never give up"
   fmt.Println("The given string is:", str)
   // Split string into a slice of substrings
   substrings := strings.Split(str, " ")
   substring := substrings[1]

   fmt.Println("The second element in the above string is:", substring)
}

Output

The given string is: Never give up
The second element in the above string is: give

Conclusion

We have successfully compiled and executed a go language program to get a substring from a given string along with examples. We have initialized three examples here in the first example we are using a separate function along with trimSpace() function while in second example we are using the concept of indexing to achieve the result. In the third example we have used the split() function present in the strings package.

Updated on: 13-Feb-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements