string.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package string
  2. import (
  3. "strconv"
  4. "strings"
  5. "unicode/utf8"
  6. )
  7. // StrLen string length
  8. // php strlen
  9. func StrLen(s string) int {
  10. return len(s)
  11. }
  12. // MbStrLen string for utf8
  13. // php mb_strlen
  14. func MbStrLen(s string) int {
  15. return utf8.RuneCountInString(s)
  16. }
  17. // SubstrCount counts the number of substr in s
  18. // php substr_count
  19. func SubstrCount(s, substr string) int {
  20. return strings.Count(s, substr)
  21. }
  22. // Substr get substr
  23. // php substr
  24. func Substr(s string, start int, length int) string {
  25. return s[start : start+length]
  26. }
  27. // MbSubstr get substr for utf8
  28. // php mb_substr
  29. func MbSubstr(s string, start int, length int) string {
  30. strRune := []rune(s)
  31. return string(strRune[start : start+length])
  32. }
  33. // StrPos get first index of substr in s, from "start" index
  34. // php strpos
  35. func StrPos(s, substr string, start int) int {
  36. return strings.Index(s[start:], substr)
  37. }
  38. // StrRPos get last index of substr in s, from "start" index
  39. // php strrpos
  40. func StrRPos(s, substr string, start int) int {
  41. return strings.LastIndex(s[start:], substr)
  42. }
  43. // StrSplit slices s into all substrings separated by sep
  44. // php str_split
  45. func StrSplit(s, sep string) []string {
  46. return strings.Split(s, sep)
  47. }
  48. // UCFirst toUpper the first letter
  49. // php ucfirst
  50. func UCFirst(s string) string {
  51. firstChart := strings.ToUpper(s[0:1])
  52. return firstChart + s[1:]
  53. }
  54. // Convert to int64
  55. func ConvertInt64(s string) int64 {
  56. cInt, err := strconv.ParseInt(s, 10, 64)
  57. if err != nil {
  58. return 0
  59. }
  60. return cInt
  61. }