str

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Package str provides utility functions for string manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Camelcase

func Camelcase(s string) string

Camelcase converts a string to camelCase. Example: "foo bar" -> "fooBar"

func Capitalize

func Capitalize(s string) string

Capitalize capitalizes the first character of a string. Example: "fred" -> "Fred"

func Contains

func Contains(s, substr string) bool

Contains checks if a string contains a substring. Example: Contains("abc", "b") -> true

func Count

func Count(s, substr string) int

Count counts the occurrences of a substring in a string. Example: Count("ababab", "ab") -> 3

func Ellipsis

func Ellipsis(s string, length int) string

Ellipsis trims and truncates a string to a specified length in bytes and appends an ellipsis if truncated. It ensures that UTF-8 characters are not split in the middle. Example: Ellipsis("Hello, 世界", 8) -> "Hello, ..."

func EndsWith

func EndsWith(s, target string) bool

EndsWith checks if a string ends with the given target string. Example: EndsWith("abc", "c") -> true

func Index

func Index(s, substr string) int

Index returns the index of the first occurrence of a substring in a string. Returns -1 if the substring is not found. Example: Index("abc", "b") -> 1

func Join

func Join(arr []string, separator string) string

Join joins an array of strings with the given separator. Example: Join(["a", "b", "c"], "-") -> "a-b-c"

func KebabCase

func KebabCase(s string) string

KebabCase converts string to kebab case. Example: "hello world" -> "hello-world" Example: "HelloWorld" -> "hello-world" Example: "HELLO_WORLD" -> "hello-world"

func LastIndex

func LastIndex(s, substr string) int

LastIndex returns the index of the last occurrence of a substring in a string. Returns -1 if the substring is not found. Example: LastIndex("abcabc", "b") -> 4

func PascalCase

func PascalCase(s string) string

PascalCase converts string to pascal case. Example: "hello world" -> "HelloWorld" Example: "hello-world" -> "HelloWorld" Example: "hello_world" -> "HelloWorld"

func Repeat

func Repeat(s string, n int) string

Repeat repeats a string n times. Example: Repeat("abc", 2) -> "abcabc"

func Replace

func Replace(s, old, new string) string

Replace replaces all occurrences of a substring in a string. Example: Replace("Hi Fred", "Fred", "Barney") -> "Hi Barney"

func RuneLength

func RuneLength(str string) int

RuneLength returns the number of runes in a string. Example: RuneLength("Hello, 世界") -> 8

func SnakeCase

func SnakeCase(s string) string

SnakeCase converts string to snake case. Example: "hello world" -> "hello_world" Example: "HelloWorld" -> "hello_world" Example: "HELLO-WORLD" -> "hello_world"

func Split

func Split(s, separator string) []string

Split splits a string by the given separator. Example: Split("a-b-c", "-") -> ["a", "b", "c"]

func StartsWith

func StartsWith(s, target string) bool

StartsWith checks if a string starts with the given target string. Example: StartsWith("abc", "a") -> true

func ToLower

func ToLower(s string) string

ToLower converts a string to lowercase. Example: ToLower("ABC") -> "abc"

func ToString

func ToString[T any](value T) string

ToString converts any value to its string representation. Example: ToString(123) -> "123"

func ToUpper

func ToUpper(s string) string

ToUpper converts a string to uppercase. Example: ToUpper("abc") -> "ABC"

func Trim

func Trim(s string, cutset ...string) string

Trim removes leading and trailing whitespace or specified characters from a string. Example: Trim(" abc ") -> "abc"

func TrimEnd

func TrimEnd(s string, cutset ...string) string

TrimEnd removes trailing whitespace or specified characters from a string. Example: TrimEnd(" abc ") -> " abc"

func TrimStart

func TrimStart(s string, cutset ...string) string

TrimStart removes leading whitespace or specified characters from a string. Example: TrimStart(" abc ") -> "abc "

func Words

func Words(str string) []string

Words splits string into an array of its words.

func WordsPattern

func WordsPattern(s, pattern string) []string

WordsPattern splits string into words using a custom pattern. Example: WordsPattern("hello-world_test", `[\-_]+`) -> ["hello", "world", "test"]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL