> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/48-string-functions.md).

# 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:

```go
// 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:

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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/48-string-functions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
