Golang Program to get characters from a string using the index


In Golang, there are various methods to get characters from a string with the help of an index. In this article, we are using external functions as well as by using internal functions defined in the go language.

Method 1: Using User-Defined Function

In this method, the function that we create will take the string whose character is to be determined along with the integer type index of the string and will return the character of the string present at that index.

Algorithm

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

  • Step 2 − Then, create a function that will take the string whose character is to be determined as an argument along with the index.

  • Step 3 − Start the main() function.

  • Step 4 − Inside the main() initialize a variable of type string and assign a value to it.

  • Step 5 − Print the variable on the screen.

  • Step 6 − Initialize a variable of type int and assign it an index value

  • Step 7 − Now, call the getChar() function by passing the character as well as the index as arguments to the function.

  • Step 8 − Store the result obtained by the function in a variable in string format.

  • Step 9 − Print this value on the screen by using fmt.Println() function.

Example

In this example we will use a user defined external function to get the characters from the strings

package main
import "fmt"

// function to get characters from strings using index
func getChar(str string, index int) rune {
	return []rune(str)[index]
}
func main() {
   // initializing a string
   var str string = "Prevention is better than cure"
   fmt.Println("The given string is:", str)
   var index int = 24
   var result string = string(getChar(str, index))
   fmt.Println("The character at index", index, "is:", result)
}

Output

The given string is: Prevention is better than cure
The character at index 24 is: n

Method 2: Using Split() Function

We will use the internal function in order to get the array of characters present in the string then by using the index we can grab the character value present at that index.

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.

func Index(s, substr string) int

The index() function is present in strings package and is used to get the index of the first occurrence of a given substring. The function takes two values as arguments. One is the string and other is the substring whose occurrence is to be detected. The function then returns the first occurrence of that substring in integer format.

Algorithm

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

  • Step 2 − Then, start the main() function. Inside the main() initialize a variable of type string and assign a value to it.

  • Step 3 − Further, print the string on the screen and assign a variable of type int and store an integer value at which the character is to be obtained.

  • Step 4 − Now, use the split() function present in strings package to get the array of characters of the given string.

  • Step 5 − Now, use the index initialized above to get the character present at that value from the array of characters and store that character in a separate variable.

  • Step 6 − We have now achieved the character that we wanted to get. Print the character on the screen by using fmt.Println() function.

Example 1

In this example we will use the split() function present in strings package in order to convert the characters from the string.

package main
import (
	"fmt"
	"strings"
)
func main() {
   // initializing a string
   var str string = "Prevention is better than cure"
   fmt.Println("The given string is:", str)
   var index int = 7
   chars := strings.Split(str, "")
   var result string = chars[index]
   fmt.Println("The character at index", index, "is:", result)
}

Output

The given string is: Prevention is better than cure
The character at index 7 is: i

Example 2

package main
import (
	"fmt"
	"strings"
)
func main() {
   // initializing a string
   var str string = "Prevention is better than cure"
   fmt.Println("The given string is:", str)
   var index int = 17

   var result string = string(str[strings.Index(str, string(str[index]))])
   fmt.Println("The character at index", index, "is:", result)
}

Output

The given string is: Prevention is better than cure
The character at index 17 is: t

Conclusion

We have successfully compiled and executed a Go language Program to get characters from a string using the index along with examples. Here we have compiled three examples in the first one we are using an external function to achieve the result while in the second and third we are using library functions present in go language for the same.

Updated on: 13-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements