For the complete documentation index, see llms.txt. This page is also available as Markdown.

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

Explanation:

  1. s.Contains("test", "es"):

    • Checks if the string "test" contains the substring "es."

  2. s.Count("test", "t"):

    • Counts the occurrences of the character "t" in the string "test."

  3. s.HasPrefix("test", "te"):

    • Checks if the string "test" has the prefix "te."

  4. s.HasSuffix("test", "st"):

    • Checks if the string "test" has the suffix "st."

  5. s.Index("test", "e"):

    • Finds the index of the first occurrence of the substring "e" in the string "test."

  6. s.Join([]string{"a", "b"}, "-"):

    • Joins the strings "a" and "b" with the separator "-."

  7. s.Repeat("a", 5):

    • Repeats the string "a" five times.

  8. s.Replace("foo", "o", "0", -1):

    • Replaces all occurrences of the substring "o" with "0" in the string "foo."

  9. s.Replace("foo", "o", "0", 1):

    • Replaces the first occurrence of the substring "o" with "0" in the string "foo."

  10. s.Split("a-b-c-d-e", "-"):

    • Splits the string "a-b-c-d-e" into a slice based on the separator "-."

  11. s.ToLower("TEST"):

    • Converts the string "TEST" to lowercase.

  12. 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