Algorithm
In Go (also known as Golang), strings are a sequence of bytes (characters) representing Unicode characters. Strings in Go are immutable, which means that once a string is created, it cannot be changed. Here are some basic aspects of working with strings in Go:
String Declaration and Initialization:
You can declare and initialize a string using double quotes:
go
var str1 string = "Hello, Go!"
str2 := "Another string"
String Concatenation:
You can concatenate strings using the +
operator:
go
concatenated := str1 + " " + str2
String Length:
To get the length of a string, you can use the len
function:
go
length := len(str1)
Accessing Characters:
You can access individual characters of a string using indexing:
go
firstChar := str1[0] // Access the first character
String Slicing:
You can obtain substrings using slicing:
go
substring := str1[1:5] // Get characters from index 1 to 4 (excluding 5)
Strings and Unicode:
Go supports Unicode characters, and strings can contain characters from any Unicode code point.
go
unicodeStr := "こんにちは世界" // Hello, world in Japanese
Code Examples
#1 Golang Programming String
Code -
Golang Programming
// Program to create a string in Golang
package main
import "fmt"
func main() {
// create string using var
var message1 = "Hello,"
// create string using shorthand notation
message2 := "Welcome to DevsEnv"
fmt.Println(message1)
fmt.Println(message2)
}
Copy The Code &
Try With Live Editor
Output
Welcome to DevsEnv
#2 Golang String using backtick (` `)
Code -
Golang Programming
Program to represent a string with a backtick
package main
import "fmt"
func main() {
// represent string with ` `
message := `I love Go Programming`
fmt.Println(message)
}
Copy The Code &
Try With Live Editor
Output
#3 Access Characters of String in Go Lang Programming
Code -
Golang Programming
// Program to access individual character of string
package main
import "fmt"
func main() {
// create and initialize a string
name := "Programiz"
// access first character
fmt.Printf("%c\n", name[0]) // P
// access fourth character
fmt.Printf("%c\n", name[3]) // g
// access last character
fmt.Printf("%c", name[8]) // z
}
Copy The Code &
Try With Live Editor
#4 Find the length of a string GoLang Programming
Code -
Golang Programming
// Program to count the length of a string
package main
import "fmt"
func main() {
// create string
message := "Welcome to Programiz"
// use len() function to count length
stringLength := len(message)
fmt.Println("Length of a string is:", len(stringLength))
}
Copy The Code &
Try With Live Editor
Output
#5 Join Two Strings Together
Code -
Golang Programming
// Program to concatenate two strings
package main
import "fmt"
func main() {
message1 := "I love"
message2 := "Go programming"
// concatenation using + operator
result := message1 + " " + message2
fmt.Println(result)
}
Copy The Code &
Try With Live Editor
Output
#6 Compare Two Strings in Golang programming
Code -
Golang Programming
// Program to compare string using Compare()
package main
import (
"fmt"
"strings"
)
func main() {
// create three strings
string1 := "Programiz"
string2 := "Programiz Pro"
string3 := "Programiz"
// compare strings
fmt.Println(strings.Compare(string1, string2)) // -1
fmt.Println(strings.Compare(string2, string3)) // 1
fmt.Println(strings.Compare(string1, string3)) // 0
}
Copy The Code &
Try With Live Editor
Output
#7 Check if String contains Substring
Code -
Golang Programming
// Program to illustrate the use of Contains()
package main
import (
"fmt"
"strings"
)
func main() {
text := "Go Programming"
substring1 := "Go"
substring2 := "Golang"
// check if Go is present in Go Programming
result := strings.Contains(text, substring1)
fmt.Println(result)
// check if Golang is present in Go Programming
result = strings.Contains(text, substring2)
fmt.Println(result)
}
Copy The Code &
Try With Live Editor
Output
false
#8 Replace a String in Golang Programming
Code -
Golang Programming
// Program using Replace() to replace strings
package main
import (
"fmt"
"strings"
)
func main() {
text := "car"
fmt.Println("Old String:", text)
// replace r with t
replacedText := strings.Replace(text, "r", "t", 1)
fmt.Println("New String:", replacedText)
}
Copy The Code &
Try With Live Editor
Output
New String: cat
#9 Change Case of Go String
Code -
Golang Programming
// Program to convert a string to uppercaseand lowercase
package main
import (
"fmt"
"strings"
)
func main() {
text1 := "go is fun to learn"
// convert to uppercase
text1 = strings.ToUpper(text1)
fmt.Println(text1)
text2 := "I LOVE GOLANG"
// convert to lowercase
text2 = strings.ToLower(text2)
fmt.Println(text2)
}
Copy The Code &
Try With Live Editor
Output
i love golang
#10 Escape Sequence in Golang String
Code -
Golang Programming
package main
import "fmt"
func main() {
// use escape character in string
message := "This article is about \"String\" in Go Programming."
fmt.Println(message)
}
// Output: This article is about "String" in Go Programming.
Copy The Code &
Try With Live Editor
Demonstration
Golang pragramming String