-
-
Notifications
You must be signed in to change notification settings - Fork 687
Update strings-package concept docs
#3061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| { | ||
| "blurb": "The standard library strings package includes great utility functions to work with strings.", | ||
| "authors": ["tehsphinx", "erikschierboom"], | ||
| "contributors": [] | ||
| "blurb": "The standard library \"strings\" package includes utility functions to work with strings.", | ||
| "authors": [ | ||
| "tehsphinx", | ||
| "erikschierboom" | ||
| ], | ||
| "contributors": [ | ||
| "BNAndras" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,44 @@ | ||
| # About | ||
|
|
||
| The [`strings` package](https://pkg.go.dev/strings) contains many useful functions to work on strings: | ||
| The [`strings` package](https://pkg.go.dev/strings) provides functions for working with strings. | ||
| Below are some of the functions in the package; check out the documentation to see the rest! | ||
|
|
||
| | Function | Purpose | | ||
| | --------------------------------------------------- | ------------------------------------------------------------------- | | ||
| | [ToLower](https://pkg.go.dev/strings#ToLower) | Convert the string to lower case | | ||
| | [ToUpper](https://pkg.go.dev/strings#ToUpper) | Convert the string to upper case | | ||
| | [TrimSpace](https://pkg.go.dev/strings#TrimSpace) | Trim leading and trailing whitespace | | ||
| | [Index](https://pkg.go.dev/strings#Index) | Find the index of the first instance of a substring within a string | | ||
| | [Replace](https://pkg.go.dev/strings#Replace) | Replace one occurrence of a substring in a string | | ||
| | [ReplaceAll](https://pkg.go.dev/strings#ReplaceAll) | Replace all occurrences of a substring in a string | | ||
| | [Split](https://pkg.go.dev/strings#Split) | Split a string into parts based on a separator | | ||
| | [HasSuffix](https://pkg.go.dev/strings#HasSuffix) | Check if a string ends with a specific substring | | ||
| | [Count](https://pkg.go.dev/strings#Count) | Count the number of occurrences of a substring within a string | | ||
| | Role | Function | Purpose | | ||
| | --------------------- | --------------------------------------------------- | ------------------------------------------------------------------- | | ||
| | Case Conversion | [ToLower](https://pkg.go.dev/strings#ToLower) | Convert a string to lower case | | ||
| | Case Conversion | [ToUpper](https://pkg.go.dev/strings#ToUpper) | Convert a string to upper case | | ||
| | Searching | [Contains](https://pkg.go.dev/strings#Contains) | Check if a string contains a specific substring | | ||
| | Searching | [Count](https://pkg.go.dev/strings#Count) | Count non-overlapping occurrences of a substring | | ||
| | Searching | [HasPrefix](https://pkg.go.dev/strings#HasPrefix) | Check if a string starts with a specific substring | | ||
| | Searching | [HasSuffix](https://pkg.go.dev/strings#HasSuffix) | Check if a string ends with a specific substring | | ||
| | Searching | [Index](https://pkg.go.dev/strings#Index) | Return the index of the first occurrence of a substring, or -1 | | ||
| | Modifying | [Replace](https://pkg.go.dev/strings#Replace) | Replace up to n occurrences of a substring | | ||
| | Modifying | [ReplaceAll](https://pkg.go.dev/strings#ReplaceAll) | Replace all occurrences of a substring | | ||
| | Modifying | [TrimSpace](https://pkg.go.dev/strings#TrimSpace) | Remove leading and trailing whitespace | | ||
| | Splitting or Joining | [Join](https://pkg.go.dev/strings#Join) | Join a slice of strings with a separator | | ||
| | Splitting or Joining | [Split](https://pkg.go.dev/strings#Split) | Split a string into parts on a separator | | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fine by me, but the additional code examples might need some explanation given that range iteration is covered way later in the syllabus. Slices are also later in the syllabus but they're not as far so they're probably okay.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sigh Building a syllabus where you can only mention prior topics is hard. I think a comment mentioning "Don't worry about this syntax too much; we discuss it in the range concept later" should suffice. |
||
|
|
||
| ```go | ||
| import "strings" | ||
|
|
||
| strings.ToLower("Gopher") // => "gopher" | ||
| strings.Index("Apple", "le") // => 3 | ||
| strings.Count("test", "t") // => 2 | ||
| // Case conversion | ||
| strings.ToLower("Gopher") // "gopher" | ||
| strings.ToUpper("Gopher") // "GOPHER" | ||
|
|
||
| // Searching | ||
| strings.Contains("Apple", "le") // true | ||
| strings.Index("Apple", "le") // 3 | ||
| strings.Index("Apple", "xyz") // -1 | ||
| strings.Count("test", "t") // 2 | ||
| strings.HasPrefix("Gopher", "Go") // true | ||
| strings.HasSuffix("Gopher", "er") // true | ||
|
|
||
| // Modifying | ||
| strings.TrimSpace(" hello ") // "hello" | ||
| strings.Replace("aabbcc", "b", "x", 1) // "aaxbcc" | ||
| strings.ReplaceAll("aabbcc", "b", "x") // "aaxxcc" | ||
|
|
||
| // Splitting or Joining | ||
| strings.Split("a,b,c", ",") // []string{"a", "b", "c"} | ||
| strings.Join([]string{"a", "b", "c"}, ",") // "a,b,c" | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,41 @@ | ||
| # Introduction | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the files be synced? |
||
|
|
||
| The `strings` package contains many useful functions to work on strings. | ||
| The [`strings` package](https://pkg.go.dev/strings) provides functions for working with strings: | ||
|
|
||
| | Role | Function | Purpose | | ||
| | --------------------- | --------------------------------------------------- | ------------------------------------------------------------------- | | ||
| | Case Conversion | [ToLower](https://pkg.go.dev/strings#ToLower) | Convert a string to lower case | | ||
| | Case Conversion | [ToUpper](https://pkg.go.dev/strings#ToUpper) | Convert a string to upper case | | ||
| | Searching | [Contains](https://pkg.go.dev/strings#Contains) | Check if a string contains a specific substring | | ||
| | Searching | [Count](https://pkg.go.dev/strings#Count) | Count non-overlapping occurrences of a substring | | ||
| | Searching | [HasSuffix](https://pkg.go.dev/strings#HasSuffix) | Check if a string ends with a specific substring | | ||
| | Searching | [Index](https://pkg.go.dev/strings#Index) | Return the index of the first occurrence of a substring, or -1 | | ||
| | Modifying | [Replace](https://pkg.go.dev/strings#Replace) | Replace up to n occurrences of a substring | | ||
| | Modifying | [ReplaceAll](https://pkg.go.dev/strings#ReplaceAll) | Replace all occurrences of a substring | | ||
| | Modifying | [TrimSpace](https://pkg.go.dev/strings#TrimSpace) | Remove leading and trailing whitespace | | ||
| | Splitting or Joining | [Join](https://pkg.go.dev/strings#Join) | Join a slice of strings with a separator | | ||
| | Splitting or Joining | [Split](https://pkg.go.dev/strings#Split) | Split a string into parts on a separator | | ||
|
|
||
| ```go | ||
| import "strings" | ||
|
|
||
| strings.ToUpper("test") // => "TEST" | ||
| // Case conversion | ||
| strings.ToLower("Gopher") // "gopher" | ||
| strings.ToUpper("Gopher") // "GOPHER" | ||
|
|
||
| // Searching | ||
| strings.Contains("Apple", "le") // true | ||
| strings.Index("Apple", "le") // 3 | ||
| strings.Index("Apple", "xyz") // -1 | ||
| strings.Count("test", "t") // 2 | ||
| strings.HasSuffix("Gopher", "er") // true | ||
|
|
||
| // Modifying | ||
| strings.TrimSpace(" hello ") // "hello" | ||
| strings.Replace("aabbcc", "b", "x", 1) // "aaxbcc" | ||
| strings.ReplaceAll("aabbcc", "b", "x") // "aaxxcc" | ||
|
|
||
| // Splitting or Joining | ||
| strings.Split("a,b,c", ",") // []string{"a", "b", "c"} | ||
| strings.Join([]string{"a", "b", "c"}, ",") // "a,b,c" | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.