github.com/gogf/gf@v1.16.9/text/gstr/gstr_substr.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  // SubStr returns a portion of string `str` specified by the `start` and `length` parameters.
    10  // The parameter `length` is optional, it uses the length of `str` in default.
    11  func SubStr(str string, start int, length ...int) (substr string) {
    12  	strLength := len(str)
    13  	// Simple border checks.
    14  	if start < 0 {
    15  		start = 0
    16  	}
    17  	if start >= strLength {
    18  		start = strLength
    19  	}
    20  	end := strLength
    21  	if len(length) > 0 {
    22  		end = start + length[0]
    23  		if end < start {
    24  			end = strLength
    25  		}
    26  	}
    27  	if end > strLength {
    28  		end = strLength
    29  	}
    30  	return str[start:end]
    31  }
    32  
    33  // SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters.
    34  // SubStrRune considers parameter `str` as unicode string.
    35  // The parameter `length` is optional, it uses the length of `str` in default.
    36  func SubStrRune(str string, start int, length ...int) (substr string) {
    37  	// Converting to []rune to support unicode.
    38  	var (
    39  		runes       = []rune(str)
    40  		runesLength = len(runes)
    41  	)
    42  
    43  	// Simple border checks.
    44  	if start < 0 {
    45  		start = 0
    46  	}
    47  	if start >= runesLength {
    48  		start = runesLength
    49  	}
    50  	end := runesLength
    51  	if len(length) > 0 {
    52  		end = start + length[0]
    53  		if end < start {
    54  			end = runesLength
    55  		}
    56  	}
    57  	if end > runesLength {
    58  		end = runesLength
    59  	}
    60  	return string(runes[start:end])
    61  }
    62  
    63  // StrLimit returns a portion of string `str` specified by `length` parameters, if the length
    64  // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
    65  func StrLimit(str string, length int, suffix ...string) string {
    66  	if len(str) < length {
    67  		return str
    68  	}
    69  	suffixStr := defaultSuffixForStrLimit
    70  	if len(suffix) > 0 {
    71  		suffixStr = suffix[0]
    72  	}
    73  	return str[0:length] + suffixStr
    74  }
    75  
    76  // StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length
    77  // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
    78  // StrLimitRune considers parameter `str` as unicode string.
    79  func StrLimitRune(str string, length int, suffix ...string) string {
    80  	runes := []rune(str)
    81  	if len(runes) < length {
    82  		return str
    83  	}
    84  	suffixStr := defaultSuffixForStrLimit
    85  	if len(suffix) > 0 {
    86  		suffixStr = suffix[0]
    87  	}
    88  	return string(runes[0:length]) + suffixStr
    89  }