strUtil

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaesarCipher

func CaesarCipher(text string, shift int) string

CaesarCipher 实现凯撒密码加密/解密

只对字母字符(A-Z, a-z)进行转换,非字母字符保持不变。大小写字母分开处理,保持原有大小写

参数:

text: 要加密/解密的字符串
shift: 移动步长(可以为负数),负数偏移(向左移动),超出字母表范围时自动循环(Z + 1 = A,A - 1 = Z)

返回:

加密/解密后的字符串

func ChunkStringsOptimized

func ChunkStringsOptimized(slice []string, chunkSize int) [][]string

ChunkStringsOptimized 将字符串切片按指定大小分块。 参数:

  • slice: 待分块的字符串切片。
  • chunkSize: 每块的大小。

返回值:

  • [][]string: 分块后的二维字符串切片。

说明:

  • 函数会预计算分组数量以提高性能。
  • 分块逻辑确保所有元素均被包含,且每块大小不超过 chunkSize。

func GetNowDayStr

func GetNowDayStr() string

GetNowDayStr 返回当前日期的8位字符串,格式为 "20060102"。

func GetNowStr

func GetNowStr(layout string) string

GetNowStr 根据指定的时间格式字符串(layout),返回当前时间的格式化字符串。 参数:

  • layout: 时间格式字符串,例如 "2006-01-02 15:04:05"。

返回值:

  • string: 当前时间的格式化字符串。

func GetNowTimeStr

func GetNowTimeStr() string

GetNowTimeStr 返回当前时间的6位字符串,格式为 "150405"。

func IsNumeric

func IsNumeric(s string) bool

IsNumeric 检查字符串是否只包含数字字符 参数 s 是要检查的字符串 返回 true 如果字符串非空且只包含数字字符,否则返回 false

func IsValidDate

func IsValidDate(dateStr string) bool

IsValidDate 检查日期字符串是否有效,支持6位(YYYYMM)或8位(YYYYMMDD)格式 如果是6位格式,会自动补全为8位格式(YYYYMM01)进行验证

func IsValidDate6

func IsValidDate6(dateStr string) bool

IsValidDate6 检查6位日期字符串(格式为YYYYMM)是否有效

func ParseMap

func ParseMap(entries []string) map[string]string

parseMap 将字符串数组解析为键值对映射。 参数:

  • entries: 包含键值对字符串的数组,格式为"key:value"。

返回值:

  • map[string]string: 解析后的键值对映射。

func SplitDateStr

func SplitDateStr(dateStr string) (int, int, int, error)

SplitDateStr 将格式为YYMMDD或YYYYMMDD的日期字符串拆分为年、月、日三个整数值 参数dateStr: 需要解析的日期字符串 返回值: 年、月、日三个整数值,以及可能的错误信息 错误: 当日期格式无效或解析失败时返回错误

func StrSliceToStruct

func StrSliceToStruct(data []string, target any) error

StrSliceToStruct 将字符串切片映射到目标结构体的字段中。

参数:

  • data: 字符串切片,包含需要映射到结构体的数据。
  • target: 目标结构体的指针,其字段将通过反射被填充。

返回值:

  • error: 如果映射过程中发生错误(如索引越界或不支持的类型),返回错误信息。

说明:

  • 结构体字段必须使用 `field` 标签指定字符串切片中的索引位置(从0开始)。
  • 支持的类型包括:string、int、float64 和 bool。
  • 如果字段的索引超出字符串切片的范围,会返回错误。
  • 如果字段类型不支持,会返回错误。

示例:

type Example struct {
    Name string `field:"0"`
    Age  int    `field:"1"`
}
var ex Example
err := StrSliceToStruct([]string{"Alice", "30"}, &ex)
if err != nil {
    log.Fatal(err)
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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