Skip to content

Instantly share code, notes, and snippets.

@Ikhan
Created September 1, 2023 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ikhan/7a7c5dde358e1e314053d31208f762fe to your computer and use it in GitHub Desktop.
Save Ikhan/7a7c5dde358e1e314053d31208f762fe to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strings"
s "strings"
)
func main() {
//to check give string has a substring
fmt.Println(s.Contains("steve", "eve"))
//to check no.of non-overlapping occurances os substring
fmt.Println(s.Count("steve", "e"))
//to check given string has a specific prefix
fmt.Println(s.HasPrefix("Mr.Steve", "Mr"))
//to check give give has a suffix
fmt.Println(s.HasSuffix("Mrs.Steve Roger", "Khan"))
//index
fmt.Println("index", s.Index("steve", "e"))
//join
fmt.Println("Join: ", s.Join([]string{"a", "b"}, "-"))
//repeat
fmt.Println("Repeat : ", s.Repeat("a", 5))
//split
fmt.Println("split", s.Split("a-b-c-d-e-f", "-"))
//lower
fmt.Println("toLower", s.ToLower("STEVE"))
//upper
fmt.Println("toUpper", s.ToUpper("steve"))
//title
fmt.Println("title", s.Title("steve"))
//trim
str := " ----go is amazing!!---"
trimChars := " -!"
trimmedString := s.Trim(str, trimChars)
fmt.Println("trimmed string:", trimmedString)
//fields
string := "go is an awesome lang"
words := s.Fields(string)
fmt.Println(words)
//index of first occurance
stringIdx := "go is a great proramming language"
firstIdx := s.IndexAny(stringIdx, "p")
fmt.Println(firstIdx)
//index of last occurance
lastIdx := s.LastIndexAny(stringIdx, "pro")
fmt.Println(lastIdx)
//Map
strMapped := "hello"
mappedString := s.Map(func(r rune) rune {
return r + 1
}, strMapped)
fmt.Println("mapped string:", mappedString)
//compare
strComp1 := "apple"
strComp2 := "banana"
comparisonResult := s.Compare(strComp1, strComp2)
fmt.Println("comparison results", comparisonResult)
//reader
strReader := "Hello nerds"
reader := strings.NewReader(strReader)
for {
char, err := reader.ReadByte()
if err != nil {
break
}
fmt.Printf("%c", char)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment