2025-10-26 20:56:03 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
|
|
func FindWithIndex[T any](slice []T, predicate func(item T) bool) (int, *T, bool) {
|
|
|
|
|
|
for i := range slice {
|
|
|
|
|
|
if predicate(slice[i]) {
|
|
|
|
|
|
return i, &slice[i], true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return -1, nil, false
|
|
|
|
|
|
}
|
2025-11-23 23:38:03 +00:00
|
|
|
|
func LastFourElements[T any](s []T, n1 int) []T {
|
|
|
|
|
|
n := len(s)
|
|
|
|
|
|
if n <= n1 {
|
|
|
|
|
|
// 切片长度小于等于4时,返回整个切片
|
|
|
|
|
|
return s
|
|
|
|
|
|
}
|
|
|
|
|
|
// 切片长度大于4时,返回最后4个元素(从n-4索引到末尾)
|
|
|
|
|
|
return s[n-n1:]
|
|
|
|
|
|
}
|
2025-11-25 16:36:55 +08:00
|
|
|
|
func RemoveLast(s string) string {
|
|
|
|
|
|
if s == "" {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
runes := []rune(s)
|
|
|
|
|
|
return string(runes[:len(runes)-1])
|
|
|
|
|
|
}
|