String Functions
This Go code demonstrates various string manipulation functions from the strings
package. Let's go through the code with inline comments and explanations:
// Importing necessary packages.
import (
"fmt"
s "strings" // Importing the strings package and aliasing it as 's'.
)
// Alias for fmt.Println for brevity.
var p = fmt.Println
// The main function, where the execution of the program begins.
func main() {
// Using various string manipulation functions from the strings package.
// Checking if a string contains a substring.
p("Contains: ", s.Contains("test", "es"))
// Counting occurrences of a character in a string.
p("Count: ", s.Count("test", "t"))
// Checking if a string has a specified prefix.
p("HasPrefix: ", s.HasPrefix("test", "te"))
// Checking if a string has a specified suffix.
p("HasSuffix: ", s.HasSuffix("test", "st"))
// Finding the index of the first occurrence of a substring in a string.
p("Index: ", s.Index("test", "e"))
// Joining strings in a slice with a specified separator.
p("Join: ", s.Join([]string{"a", "b"}, "-"))
// Repeating a string a specified number of times.
p("Repeat: ", s.Repeat("a", 5))
// Replacing occurrences of a substring with another substring.
p("Replace: ", s.Replace("foo", "o", "0", -1))
p("Replace: ", s.Replace("foo", "o", "0", 1))
// Splitting a string into a slice based on a specified separator.
p("Split: ", s.Split("a-b-c-d-e", "-"))
// Converting a string to lowercase.
p("ToLower: ", s.ToLower("TEST"))
// Converting a string to uppercase.
p("ToUpper: ", s.ToUpper("test"))
}
Output
Contains: true
Count: 2
HasPrefix: true
HasSuffix: true
Index: 1
Join: a-b
Repeat: aaaaa
Replace: f00
Replace: f0o
Split: [a b c d e]
ToLower: test
ToUpper: TEST
Explanation:
s.Contains("test", "es")
:Checks if the string "test" contains the substring "es."
s.Count("test", "t")
:Counts the occurrences of the character "t" in the string "test."
s.HasPrefix("test", "te")
:Checks if the string "test" has the prefix "te."
s.HasSuffix("test", "st")
:Checks if the string "test" has the suffix "st."
s.Index("test", "e")
:Finds the index of the first occurrence of the substring "e" in the string "test."
s.Join([]string{"a", "b"}, "-")
:Joins the strings "a" and "b" with the separator "-."
s.Repeat("a", 5)
:Repeats the string "a" five times.
s.Replace("foo", "o", "0", -1)
:Replaces all occurrences of the substring "o" with "0" in the string "foo."
s.Replace("foo", "o", "0", 1)
:Replaces the first occurrence of the substring "o" with "0" in the string "foo."
s.Split("a-b-c-d-e", "-")
:Splits the string "a-b-c-d-e" into a slice based on the separator "-."
s.ToLower("TEST")
:Converts the string "TEST" to lowercase.
s.ToUpper("test")
:Converts the string "test" to uppercase.
These functions provide a wide range of string manipulation capabilities, making it convenient to work with strings in Go.
Last updated
Was this helpful?