stringx

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 5 Imported by: 5

Documentation

Overview

Additional functions for string processing that are missing in the standard library.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Capitalize

func Capitalize(str string, all bool) string

Capitalizes the given string, if `all` is true, all words are capitalized, otherwise only the first word will be capitalized.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str1 := "hello, world"
	str2 := stringx.Capitalize(str1, false)
	str3 := stringx.Capitalize(str1, true)
	str4 := stringx.Capitalize(" hello world", false)
	str5 := stringx.Capitalize("  hello    world", true)
	str6 := stringx.Capitalize("你好,世界!", false) // this doesn't effect since it contains no latin characters

	fmt.Println(str2)
	fmt.Println(str3)
	fmt.Printf("%#v\n", str4)
	fmt.Printf("%#v\n", str5)
	fmt.Println(str6)
}
Output:

Hello, world
Hello, World
" Hello world"
"  Hello    World"
你好,世界!

func Chunk

func Chunk(str string, length int) []string

Breaks the string into smaller chunks according to the given length.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str := "foobar"
	chunks1 := stringx.Chunk(str, 2)
	chunks2 := stringx.Chunk(str, 4)

	fmt.Println(chunks1)
	fmt.Println(chunks2)
}
Output:

[fo ob ar]
[foob ar]

func EndsWith

func EndsWith(str string, sub string) bool

Checks if the given string ends with the specified sub string.

This function is the same as `strings.HasSuffix()`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str := "Hello, World"

	fmt.Println(stringx.EndsWith(str, "World"))
	fmt.Println(stringx.EndsWith(str, "Hello"))
}
Output:

true
false

func Hyphenate

func Hyphenate(str string) string

Replaces the spaces between non-empty characters of the given string with hyphens (`-`).

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str1 := stringx.Hyphenate("hello world")
	str2 := stringx.Hyphenate(" hello  world   ")

	fmt.Println(str1)
	fmt.Printf("%#v\v", str2)
}
Output:

hello-world
" hello-world   "

func Match

func Match(str string, patten string) []string

Retrieves the first result (with sub matches) of matching the string against a regular expression. If no match, this function returns nil.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str := "Hello, World!"
	match1 := stringx.Match(str, "l{2,}")
	match2 := stringx.Match(str, "o{2,}")
	match3 := stringx.Match(str, "[a") // invalid regex pattern returns an empty string

	fmt.Println(match1)
	fmt.Printf("%#v\n", match2)
	fmt.Printf("%#v\n", match3)
}
Output:

[ll]
[]string(nil)
[]string(nil)

func MatchAll

func MatchAll(str string, pattern string) [][]string

Retrieves all results (with sub matches) of matching the string against a regular expression. If no match, this function returns an empty slice.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str := "Hello, World!"
	match1 := stringx.MatchAll(str, "l{2,}")
	match2 := stringx.MatchAll(str, "o{2,}")
	match3 := stringx.MatchAll(str, "[a") // invalid regex pattern returns an empty slice

	fmt.Println(match1)
	fmt.Printf("%#v\n", match2)
	fmt.Printf("%#v\n", match3)
}
Output:

[[ll]]
[][]string(nil)
[][]string(nil)

func PadEnd

func PadEnd(str string, finalLength int, padStr string) string

Pads the given string with another string (multiple times, if needed) until the resulting string reaches the final length. The padding is applied from the end of the string.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str1 := "Hello, World!"
	str2 := stringx.PadEnd(str1, 15, " ")
	str3 := stringx.PadEnd(str1, 15, "*")
	str4 := stringx.PadEnd(str1, 15, "Hi")
	str5 := stringx.PadEnd(str1, 15, "Hola")
	str6 := stringx.PadEnd(str1, 12, "**")
	str7 := stringx.PadEnd(str1, 18, "Hola")

	fmt.Printf("%#v\n", str2)
	fmt.Println(str3)
	fmt.Println(str4)
	fmt.Println(str5)
	fmt.Println(str6)
	fmt.Println(str7)
}
Output:

"Hello, World!  "
Hello, World!**
Hello, World!Hi
Hello, World!Ho
Hello, World!
Hello, World!HolaH

func PadStart

func PadStart(str string, finalLength int, padStr string) string

Pads the given string with another string (multiple times, if needed) until the resulting string reaches the final length. The padding is applied from the start of the string.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str1 := "Hello, World!"
	str2 := stringx.PadStart(str1, 15, " ")
	str3 := stringx.PadStart(str1, 15, "*")
	str4 := stringx.PadStart(str1, 15, "Hi")
	str5 := stringx.PadStart(str1, 15, "Hola")
	str6 := stringx.PadStart(str1, 12, "**")
	str7 := stringx.PadStart(str1, 18, "Hola")

	fmt.Printf("%#v\n", str2)
	fmt.Println(str3)
	fmt.Println(str4)
	fmt.Println(str5)
	fmt.Println(str6)
	fmt.Println(str7)
}
Output:

"  Hello, World!"
**Hello, World!
HiHello, World!
HoHello, World!
Hello, World!
HolaHHello, World!

func Random added in v0.4.0

func Random(length int) string

Returns a random string, the charset matches `/[0-9a-zA-Z]/`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str := stringx.Random(4)
	matches := stringx.Match(str, "[0-9a-zA-Z]{4}")

	fmt.Println(matches[0] == str)
}
Output:

true
func Search(str string, pattern string) int

Executes a search for a match between a regular expression and the string, returning the index of the first match in the string.

func Slice

func Slice(str string, start int, end int) string

Returns a section of the string selected from `start` to `end` (excluded).

If `start < 0`, it will be calculated as `Length(str) + start`.

If `end < 0`, it will be calculated as `Length(str) + end`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str1 := "Hello, World!"
	str2 := stringx.Slice(str1, 0, 2)
	str3 := stringx.Slice(str1, 0, -2)
	str4 := stringx.Slice(str1, -6, -1)
	str5 := stringx.Slice(str1, 0, 20)
	str6 := stringx.Slice(str1, 20, 25) // exceeding index return an empty string

	fmt.Println(str2)
	fmt.Println(str3)
	fmt.Println(str4)
	fmt.Println(str5)
	fmt.Printf("%#v\n", str6)
}
Output:

He
Hello, Worl
World
Hello, World!
""

func StartsWith

func StartsWith(str string, sub string) bool

Checks if the given string starts with the specified sub string.

This function is the same as `strings.HasPrefix()`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str := "Hello, World"

	fmt.Println(stringx.StartsWith(str, "Hello"))
	fmt.Println(stringx.StartsWith(str, "World"))
}
Output:

true
false

func Substring

func Substring(str string, start int, end int) string

Returns a section of the string selected from `start` to `end` (excluded).

This function is similar to the `Slice()`, except it doesn't accept negative positions.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str1 := "Hello, World!"
	str2 := stringx.Substring(str1, 0, 2)
	str3 := stringx.Substring(str1, 5, 3)
	str4 := stringx.Substring(str1, 7, 20)
	str5 := stringx.Substring(str1, -1, 5) // negative index will be reset to 0
	str6 := stringx.Substring(str1, 7, -1)

	fmt.Println(str2)
	fmt.Printf("%#v\n", str3)
	fmt.Println(str4)
	fmt.Println(str5)
	fmt.Printf("%#v\n", str6)
}
Output:

He
""
World!
Hello
""

func Truncate

func Truncate(str string, length int) string

Truncates the given string to the given length (including the ending `...`).

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str1 := "Hello, World!"
	str2 := stringx.Truncate(str1, 15)
	str3 := stringx.Truncate(str1, 12)
	str4 := stringx.Truncate(str1, 10)
	str5 := stringx.Truncate(str1, -1) // negative indexing isn't supported

	fmt.Println(str2)
	fmt.Println(str3)
	fmt.Println(str4)
	fmt.Printf("%#v\n", str5)
}
Output:

Hello, World!
Hello, Wo...
Hello, ...
""

func Words

func Words(str string) []string

Extracts words (in latin characters) from the given string.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/stringx"
)

func main() {
	str := "Hello, World!"
	words := stringx.Words(str)

	fmt.Println(words)
}
Output:

[Hello World]

Types

This section is empty.

Directories

Path Synopsis
Additional functions for processing strings in multi-byte sequence.
Additional functions for processing strings in multi-byte sequence.

Jump to

Keyboard shortcuts

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