Skip to content

Latest commit

 

History

History
161 lines (125 loc) · 2.55 KB

EXAMPLES.md

File metadata and controls

161 lines (125 loc) · 2.55 KB

Strings Function Examples

SubstringSearch

package main

import (
	"fmt"
	"github.com/kashifkhan0771/utils/strings"
)

func main() {
	options := strings.SubstringSearchOptions{
		CaseInsensitive: true,
		ReturnIndexes:   false,
	}
	result := strings.SubstringSearch("Go is Great, Go is Fun!", "go", options)
	fmt.Println(result) // Output: [Go Go]
}

Title

func main() {
	title := strings.Title("hello world")
	fmt.Println(title) // Output: Hello World
}

ToTitle

func main() {
	title := strings.ToTitle("hello world of go", []string{"of", "go"})
	fmt.Println(title) // Output: Hello World of go
}

Tokenize

func main() {
	tokens := strings.Tokenize("hello,world;this is Go", ",;")
	fmt.Println(tokens) // Output: [hello world this is Go]
}

Rot13Encode

func main() {
	encoded := strings.Rot13Encode("hello")
	fmt.Println(encoded) // Output: uryyb
}

Rot13Decode

func main() {
	decoded := strings.Rot13Decode("uryyb")
	fmt.Println(decoded) // Output: hello
}

CaesarEncrypt

func main() {
	encrypted := strings.CaesarEncrypt("hello", 3)
	fmt.Println(encrypted) // Output: khoor
}

CaesarDecrypt

func main() {
	decrypted := strings.CaesarDecrypt("khoor", 3)
	fmt.Println(decrypted) // Output: hello
}

RunLengthEncode

func main() {
	encoded := strings.RunLengthEncode("aaabbbccc")
	fmt.Println(encoded) // Output: 3a3b3c
}

RunLengthDecode

func main() {
	decoded, _ := strings.RunLengthDecode("3a3b3c")
	fmt.Println(decoded) // Output: aaabbbccc
}

IsValidEmail

func main() {
	valid := strings.IsValidEmail("[email protected]")
	fmt.Println(valid) // Output: true
}

SanitizeEmail

func main() {
	email := strings.SanitizeEmail("   [email protected]   ")
	fmt.Println(email) // Output: [email protected]
}

Reverse

func main() {
	reversed := strings.Reverse("hello")
	fmt.Println(reversed) // Output: olleh
}

CommonPrefix

func main() {
	prefix := strings.CommonPrefix("nation", "national", "nasty")
	fmt.Println(prefix) // Output: na
}

CommonSuffix

func main() {
	suffix := strings.CommonSuffix("testing", "running", "jumping")
	fmt.Println(suffix) // Output: ing
}

Truncate

func main() {
	short := Truncate("This is a long string that needs to be truncated", &TruncateOptions{Length: 20, Omission: "°°°"})
	fmt.Println(short) // Output: This is a long strin°°°

	defaultShort := Truncate("Short example", nil)
fmt.Println(defaultShort) // Output: Short examp...
}