String Functions
// 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
Last updated