stringutil

package
v0.0.0-...-bb65a4e Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelToSnake

func CamelToSnake(s string) string

CamelToSnake 将驼峰式字符串转换为下划线连接

  • PascalCase -> pascal_case
  • lowerCamelCase -> lower_camel_case
  • HTTPRequest -> http_request
  • MyID -> my_id
  • ID -> id

func ContainsAll

func ContainsAll(s string, substrs ...string) bool

ContainsAll 判断字符串 s 是否包含 substrs 中的所有子串

Examples:

ContainsAll("hello world", "hello", "world") // true
ContainsAll("hello world", "hello", "world", "123") // false

func ContainsAny

func ContainsAny(s string, substrs ...string) bool

ContainsAny 判断字符串 s 是否包含 substrs 中的任意一个子串

Examples:

ContainsAny("hello world", "world", "123") // true
ContainsAny("hello world", "123") // false

func Ellipsis

func Ellipsis(str string, maxLength int) string

Ellipsis 截取字符串,并在末尾添加省略号

Params:

str: 待截取的字符串
maxLength: 最大长度

Examples:

Ellipsis("hello world", 5) // he...
Ellipsis("你好 世界!", 3) // ...
Ellipsis("你好 世界!", 5) // 你好...

func Reverse

func Reverse(s string) string

Reverse 字符串反转

func SnakeToCamel

func SnakeToCamel(s string) string

SnakeToCamel 将下划线连接的字符串转换为小驼峰式

  • my_var -> myVar
  • http_request -> httpRequest
  • _my_var -> myVar

func SnakeToPascal

func SnakeToPascal(s string) string

SnakeToPascal 将下划线连接的字符串转换为大驼峰式(PascalCase)

  • my_var -> MyVar
  • http_request -> HttpRequest
  • _my_var -> MyVar

func TakeFirst

func TakeFirst(s string, n int) string

TakeFirst 截取字符串前 n 个 Unicode 字符(rune)

Params:

s: 字符串
maxLength: 字符长度

Examples:

Truncate("hello world", 5) // hello
Truncate("你好 世界!", 2) // 你好

func TrimFirstPrefix

func TrimFirstPrefix(s string, prefixes ...string) string

TrimFirstPrefix 移除字符串中首个匹配的前缀

Examples:

TrimFirstPrefix("hello_world", "hello", "_world") // _world

func TrimFirstSuffix

func TrimFirstSuffix(s string, suffixes ...string) string

TrimFirstSuffix 移除字符串中首个匹配的后缀

Examples:

TrimFirstSuffix("hello_world", "world", "hello_") // hello_

Types

This section is empty.

Jump to

Keyboard shortcuts

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