github.com/eframework-cn/EP.GO.UTIL@v1.0.0/xstring/xstring.go (about)

     1  //-----------------------------------------------------------------------//
     2  //                     GNU GENERAL PUBLIC LICENSE                        //
     3  //                        Version 2, June 1991                           //
     4  //                                                                       //
     5  // Copyright (C) EFramework, https://eframework.cn, All rights reserved. //
     6  // Everyone is permitted to copy and distribute verbatim copies          //
     7  // of this license document, but changing it is not allowed.             //
     8  //                   SEE LICENSE.md FOR MORE DETAILS.                    //
     9  //-----------------------------------------------------------------------//
    10  
    11  // 字符串常用函数封装,如转数字、截取、修建、替换、切割等.
    12  package xstring
    13  
    14  import (
    15  	"fmt"
    16  	"strconv"
    17  	"strings"
    18  	"unsafe"
    19  )
    20  
    21  // 字符串转int
    22  func ToInt(str string) int {
    23  	itr, _ := strconv.Atoi(str)
    24  	return itr
    25  }
    26  
    27  // int转字符串
    28  func ToString(itr int) string {
    29  	str := strconv.Itoa(itr)
    30  	return str
    31  }
    32  
    33  // int64转字符串
    34  func Int64ToString(itr int64) string {
    35  	str := strconv.FormatInt(itr, 10)
    36  	return str
    37  }
    38  
    39  // 保留指定位数的小数(参数为float32或float64)(默认保留两位小数)
    40  //	fixed: 指定小数点位数
    41  func ToFixed(float interface{}, fixed ...int) string {
    42  	tfixed := 2
    43  	if len(fixed) == 1 {
    44  		tfixed = fixed[0]
    45  	}
    46  	if tfixed < 0 {
    47  		tfixed = 0
    48  	}
    49  	if float != nil {
    50  		switch float.(type) {
    51  		case float32, float64:
    52  			return fmt.Sprintf("%."+ToString(tfixed)+"f", float)
    53  		}
    54  	}
    55  	return ""
    56  }
    57  
    58  // 字符串分割
    59  //	sep: 分割符
    60  func Split(str string, sep string) []string {
    61  	return strings.Split(str, sep)
    62  }
    63  
    64  // 找到指定字符的索引
    65  func IndexOf(str string, of string) int {
    66  	return strings.Index(str, of)
    67  }
    68  
    69  // 找到指定字符的索引(后)
    70  func LastIndexOf(str string, of string) int {
    71  	return strings.LastIndex(str, of)
    72  }
    73  
    74  // 是否以指定字符起始
    75  func StartWith(str string, of string) bool {
    76  	return strings.HasPrefix(str, of)
    77  }
    78  
    79  // 是否以指定字符结束
    80  func EndWith(str string, of string) bool {
    81  	return strings.HasSuffix(str, of)
    82  }
    83  
    84  // 是否包含指定字符
    85  func Contains(str string, of string) bool {
    86  	return strings.Contains(str, of)
    87  }
    88  
    89  // 是否为空
    90  func IsEmpty(str string) bool {
    91  	return str == ""
    92  }
    93  
    94  // 截取
    95  func Sub(str string, from int, to int) string {
    96  	rs := []rune(str)
    97  	length := len(rs)
    98  	if from < 0 || to < 0 || from > to {
    99  		return ""
   100  	}
   101  	if to > length {
   102  		to = length
   103  	}
   104  	return string(rs[from:to])
   105  }
   106  
   107  // 替换所有指定字符
   108  func Replace(str string, from string, to string) string {
   109  	return strings.ReplaceAll(str, from, to)
   110  }
   111  
   112  // 剔除多余的空格
   113  func Trim(str string) string {
   114  	return strings.Trim(str, " ")
   115  }
   116  
   117  // 字符串转字节数组
   118  func StrToBytes(s string) []byte {
   119  	x := (*[2]uintptr)(unsafe.Pointer(&s))
   120  	h := [3]uintptr{x[0], x[1], x[1]}
   121  	return *(*[]byte)(unsafe.Pointer(&h))
   122  }
   123  
   124  // 字节数组转字符串
   125  func BytesToStr(b []byte) string {
   126  	return *(*string)(unsafe.Pointer(&b))
   127  }
   128  
   129  // 字符串格式化
   130  func Format(format string, args ...interface{}) string {
   131  	return fmt.Sprintf(format, args...)
   132  }