github.com/gogf/gf/v2@v2.7.4/text/gstr/gstr_trim.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
     6  
     7  package gstr
     8  
     9  import (
    10  	"strings"
    11  
    12  	"github.com/gogf/gf/v2/internal/utils"
    13  )
    14  
    15  // Trim strips whitespace (or other characters) from the beginning and end of a string.
    16  // The optional parameter `characterMask` specifies the additional stripped characters.
    17  func Trim(str string, characterMask ...string) string {
    18  	return utils.Trim(str, characterMask...)
    19  }
    20  
    21  // TrimStr strips all the given `cut` string from the beginning and end of a string.
    22  // Note that it does not strip the whitespaces of its beginning or end.
    23  func TrimStr(str string, cut string, count ...int) string {
    24  	return TrimLeftStr(TrimRightStr(str, cut, count...), cut, count...)
    25  }
    26  
    27  // TrimLeft strips whitespace (or other characters) from the beginning of a string.
    28  func TrimLeft(str string, characterMask ...string) string {
    29  	trimChars := utils.DefaultTrimChars
    30  	if len(characterMask) > 0 {
    31  		trimChars += characterMask[0]
    32  	}
    33  	return strings.TrimLeft(str, trimChars)
    34  }
    35  
    36  // TrimLeftStr strips all the given `cut` string from the beginning of a string.
    37  // Note that it does not strip the whitespaces of its beginning.
    38  func TrimLeftStr(str string, cut string, count ...int) string {
    39  	var (
    40  		lenCut   = len(cut)
    41  		cutCount = 0
    42  	)
    43  	for len(str) >= lenCut && str[0:lenCut] == cut {
    44  		str = str[lenCut:]
    45  		cutCount++
    46  		if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
    47  			break
    48  		}
    49  	}
    50  	return str
    51  }
    52  
    53  // TrimRight strips whitespace (or other characters) from the end of a string.
    54  func TrimRight(str string, characterMask ...string) string {
    55  	trimChars := utils.DefaultTrimChars
    56  	if len(characterMask) > 0 {
    57  		trimChars += characterMask[0]
    58  	}
    59  	return strings.TrimRight(str, trimChars)
    60  }
    61  
    62  // TrimRightStr strips all the given `cut` string from the end of a string.
    63  // Note that it does not strip the whitespaces of its end.
    64  func TrimRightStr(str string, cut string, count ...int) string {
    65  	var (
    66  		lenStr   = len(str)
    67  		lenCut   = len(cut)
    68  		cutCount = 0
    69  	)
    70  	for lenStr >= lenCut && str[lenStr-lenCut:lenStr] == cut {
    71  		lenStr = lenStr - lenCut
    72  		str = str[:lenStr]
    73  		cutCount++
    74  		if len(count) > 0 && count[0] != -1 && cutCount >= count[0] {
    75  			break
    76  		}
    77  	}
    78  	return str
    79  }
    80  
    81  // TrimAll trims all characters in string `str`.
    82  func TrimAll(str string, characterMask ...string) string {
    83  	trimChars := utils.DefaultTrimChars
    84  	if len(characterMask) > 0 {
    85  		trimChars += characterMask[0]
    86  	}
    87  	var (
    88  		filtered bool
    89  		slice    = make([]rune, 0, len(str))
    90  	)
    91  	for _, char := range str {
    92  		filtered = false
    93  		for _, trimChar := range trimChars {
    94  			if char == trimChar {
    95  				filtered = true
    96  				break
    97  			}
    98  		}
    99  		if !filtered {
   100  			slice = append(slice, char)
   101  		}
   102  	}
   103  	return string(slice)
   104  }
   105  
   106  // HasPrefix tests whether the string s begins with prefix.
   107  func HasPrefix(s, prefix string) bool {
   108  	return strings.HasPrefix(s, prefix)
   109  }
   110  
   111  // HasSuffix tests whether the string s ends with suffix.
   112  func HasSuffix(s, suffix string) bool {
   113  	return strings.HasSuffix(s, suffix)
   114  }