github.com/zhongdalu/gf@v1.0.0/g/internal/strutils/strutils.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 // Package strutils provides some string functions for internal usage. 8 package strutils 9 10 import "strings" 11 12 // IsLetterUpper tests whether the given byte b is in upper case. 13 func IsLetterUpper(b byte) bool { 14 if b >= byte('A') && b <= byte('Z') { 15 return true 16 } 17 return false 18 } 19 20 // IsLetterLower tests whether the given byte b is in lower case. 21 func IsLetterLower(b byte) bool { 22 if b >= byte('a') && b <= byte('z') { 23 return true 24 } 25 return false 26 } 27 28 // IsNumeric tests whether the given string s is numeric. 29 func IsNumeric(s string) bool { 30 length := len(s) 31 if length == 0 { 32 return false 33 } 34 for i := 0; i < len(s); i++ { 35 if s[i] < byte('0') || s[i] > byte('9') { 36 return false 37 } 38 } 39 return true 40 } 41 42 // UcFirst returns a copy of the string s with the first letter mapped to its upper case. 43 func UcFirst(s string) string { 44 if len(s) == 0 { 45 return s 46 } 47 if IsLetterLower(s[0]) { 48 return string(s[0]-32) + s[1:] 49 } 50 return s 51 } 52 53 // ReplaceByMap returns a copy of <origin>, 54 // which is replaced by a map in unordered way, case-sensitively. 55 func ReplaceByMap(origin string, replaces map[string]string) string { 56 for k, v := range replaces { 57 origin = strings.Replace(origin, k, v, -1) 58 } 59 return origin 60 }