Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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

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.

PreviousRecoverNextString Formatting

Last updated 1 year ago

Was this helpful?