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:
Output
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?