Home »
Golang
Golang Strings
By IncludeHelp Last updated : October 05, 2024
Strings in Golang
- A string is a slice of bytes that is represented in Unicode text using UTF-8 encoding.
- Strings are immutable structures, i.e. they are read-only values.
- The working of strings in Golang is different from that of other programming languages as it is treated as a slice of bytes.
- Strings are enclosed in double-quotes "".
Example: "includehelp.com"
Creating a string
We can create a string in Go using the following syntax,
string_name := "string_value"
Here,
- string_name is the name given to the string.
- string_value is the value of the string.
Example:
name := "Shivang Yadav"
Example: Create and print a string
package main
import "fmt"
func main() {
// creating a String
myString := "Golang programming language"
// printing the String value
fmt.Println(myString)
}
Output:
Golang programming language
Iterating over characters of the string
String in Golang is a slice due to which the program can iterator over the string using a loop.
Example : Iterate over the characters of the string
package main
import "fmt"
func main() {
// Creating a String
myString := "Hello, world! How are you?"
fmt.Printf("The string is %s\n", myString)
// Iterating
for index, s := range myString {
fmt.Printf("index -> %d : chatacter -> %c \n", index, s)
}
}
Output:
The string is Hello, world! How are you?
index -> 0 : chatacter -> H
index -> 1 : chatacter -> e
index -> 2 : chatacter -> l
index -> 3 : chatacter -> l
index -> 4 : chatacter -> o
index -> 5 : chatacter -> ,
index -> 6 : chatacter ->
index -> 7 : chatacter -> w
index -> 8 : chatacter -> o
index -> 9 : chatacter -> r
index -> 10 : chatacter -> l
index -> 11 : chatacter -> d
index -> 12 : chatacter -> !
index -> 13 : chatacter ->
index -> 14 : chatacter -> H
index -> 15 : chatacter -> o
index -> 16 : chatacter -> w
index -> 17 : chatacter ->
index -> 18 : chatacter -> a
index -> 19 : chatacter -> r
index -> 20 : chatacter -> e
index -> 21 : chatacter ->
index -> 22 : chatacter -> y
index -> 23 : chatacter -> o
index -> 24 : chatacter -> u
index -> 25 : chatacter -> ?
Finding the length of the string
Golang provides built-in functions to find the length of a string. there are two methods that we are using to find the length of the string:
- len() method
- RuneCountlnString() method
(a) The len() method
The len() method is used to return the number of bytes present in the string.
Syntax:
len(string)
Example: Find the length of string using len() method
package main
import "fmt"
func main() {
// Creating a string
myString := "Hello, World!"
fmt.Printf("The string is %s\n", myString)
// Printing the length of string
fmt.Printf("The length of string is %d", len(myString))
}
Output:
The string is Hello, World!
The length of string is 13
(b) The RuneCountlnString() method
The RuneCountlnString() method is used to return the number of runes present in the string. It is a part of the UTF-8 package. Imported as unicode/utf8.
Syntax:
utf8.RuneCountInString(string)
Example: Find the length of string using RuneCountlnString() method
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
// Creating a string
myString := "Hello, World!"
fmt.Printf("The string is %s\n", myString)
// Printing the length of string
fmt.Printf("The length of string is %d", utf8.RuneCountInString(myString))
}
Output:
The string is Hello, World!
The length of string is 13
Concatenating Strings in Go
We can concatenate string values in Golang to create a new string consisting of both values. To perform this task Golang has a Join() method that can be used to concatenate multiple strings to one using the given separator value. The method accepts two parameters, an array of strings and a separator value. And returns a value that contains the concatenated value.
Syntax
func Join(strs []string, sep string) string
Example : Concatenate strings
package main
import (
"fmt"
"strings"
)
func main() {
strArr := []string{"Golang", "Programming", "Language"}
fmt.Println("The concatenated string is", strings.Join(strArr, " "))
}
Output:
The concatenated string is Golang Programming Language