github.com/gogf/gf/v2@v2.7.4/text/gstr/gstr_sub.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 "strings"
    10  
    11  // Str returns part of `haystack` string starting from and including
    12  // the first occurrence of `needle` to the end of `haystack`.
    13  //
    14  // This function performs exactly as function SubStr, but to implement the same function
    15  // as PHP: http://php.net/manual/en/function.strstr.php.
    16  //
    17  // Example:
    18  // Str("av.mp4", ".") -> ".mp4"
    19  func Str(haystack string, needle string) string {
    20  	if needle == "" {
    21  		return ""
    22  	}
    23  	pos := strings.Index(haystack, needle)
    24  	if pos == NotFoundIndex {
    25  		return ""
    26  	}
    27  	return haystack[pos+len([]byte(needle))-1:]
    28  }
    29  
    30  // StrEx returns part of `haystack` string starting from and excluding
    31  // the first occurrence of `needle` to the end of `haystack`.
    32  //
    33  // This function performs exactly as function SubStrEx, but to implement the same function
    34  // as PHP: http://php.net/manual/en/function.strstr.php.
    35  //
    36  // Example:
    37  // StrEx("av.mp4", ".") -> "mp4"
    38  func StrEx(haystack string, needle string) string {
    39  	if s := Str(haystack, needle); s != "" {
    40  		return s[1:]
    41  	}
    42  	return ""
    43  }
    44  
    45  // StrTill returns part of `haystack` string ending to and including
    46  // the first occurrence of `needle` from the start of `haystack`.
    47  //
    48  // Example:
    49  // StrTill("av.mp4", ".") -> "av."
    50  func StrTill(haystack string, needle string) string {
    51  	pos := strings.Index(haystack, needle)
    52  	if pos == NotFoundIndex || pos == 0 {
    53  		return ""
    54  	}
    55  	return haystack[:pos+1]
    56  }
    57  
    58  // StrTillEx returns part of `haystack` string ending to and excluding
    59  // the first occurrence of `needle` from the start of `haystack`.
    60  //
    61  // Example:
    62  // StrTillEx("av.mp4", ".") -> "av"
    63  func StrTillEx(haystack string, needle string) string {
    64  	pos := strings.Index(haystack, needle)
    65  	if pos == NotFoundIndex || pos == 0 {
    66  		return ""
    67  	}
    68  	return haystack[:pos]
    69  }
    70  
    71  // SubStr returns a portion of string `str` specified by the `start` and `length` parameters.
    72  // The parameter `length` is optional, it uses the length of `str` in default.
    73  //
    74  // Example:
    75  // SubStr("123456", 1, 2) -> "23"
    76  func SubStr(str string, start int, length ...int) (substr string) {
    77  	strLength := len(str)
    78  	if start < 0 {
    79  		if -start > strLength {
    80  			start = 0
    81  		} else {
    82  			start = strLength + start
    83  		}
    84  	} else if start > strLength {
    85  		return ""
    86  	}
    87  	realLength := 0
    88  	if len(length) > 0 {
    89  		realLength = length[0]
    90  		if realLength < 0 {
    91  			if -realLength > strLength-start {
    92  				realLength = 0
    93  			} else {
    94  				realLength = strLength - start + realLength
    95  			}
    96  		} else if realLength > strLength-start {
    97  			realLength = strLength - start
    98  		}
    99  	} else {
   100  		realLength = strLength - start
   101  	}
   102  
   103  	if realLength == strLength {
   104  		return str
   105  	} else {
   106  		end := start + realLength
   107  		return str[start:end]
   108  	}
   109  }
   110  
   111  // SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters.
   112  // SubStrRune considers parameter `str` as unicode string.
   113  // The parameter `length` is optional, it uses the length of `str` in default.
   114  //
   115  // Example:
   116  // SubStrRune("一起学习吧!", 2, 2) -> "学习"
   117  func SubStrRune(str string, start int, length ...int) (substr string) {
   118  	// Converting to []rune to support unicode.
   119  	var (
   120  		runes       = []rune(str)
   121  		runesLength = len(runes)
   122  	)
   123  
   124  	strLength := runesLength
   125  	if start < 0 {
   126  		if -start > strLength {
   127  			start = 0
   128  		} else {
   129  			start = strLength + start
   130  		}
   131  	} else if start > strLength {
   132  		return ""
   133  	}
   134  	realLength := 0
   135  	if len(length) > 0 {
   136  		realLength = length[0]
   137  		if realLength < 0 {
   138  			if -realLength > strLength-start {
   139  				realLength = 0
   140  			} else {
   141  				realLength = strLength - start + realLength
   142  			}
   143  		} else if realLength > strLength-start {
   144  			realLength = strLength - start
   145  		}
   146  	} else {
   147  		realLength = strLength - start
   148  	}
   149  	end := start + realLength
   150  	if end > runesLength {
   151  		end = runesLength
   152  	}
   153  	return string(runes[start:end])
   154  }
   155  
   156  // StrLimit returns a portion of string `str` specified by `length` parameters, if the length
   157  // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
   158  //
   159  // Example:
   160  // StrLimit("123456", 3)      -> "123..."
   161  // StrLimit("123456", 3, "~") -> "123~"
   162  func StrLimit(str string, length int, suffix ...string) string {
   163  	if len(str) < length {
   164  		return str
   165  	}
   166  	suffixStr := defaultSuffixForStrLimit
   167  	if len(suffix) > 0 {
   168  		suffixStr = suffix[0]
   169  	}
   170  	return str[0:length] + suffixStr
   171  }
   172  
   173  // StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length
   174  // of `str` is greater than `length`, then the `suffix` will be appended to the result string.
   175  // StrLimitRune considers parameter `str` as unicode string.
   176  //
   177  // Example:
   178  // StrLimitRune("一起学习吧!", 2)      -> "一起..."
   179  // StrLimitRune("一起学习吧!", 2, "~") -> "一起~"
   180  func StrLimitRune(str string, length int, suffix ...string) string {
   181  	runes := []rune(str)
   182  	if len(runes) < length {
   183  		return str
   184  	}
   185  	suffixStr := defaultSuffixForStrLimit
   186  	if len(suffix) > 0 {
   187  		suffixStr = suffix[0]
   188  	}
   189  	return string(runes[0:length]) + suffixStr
   190  }
   191  
   192  // SubStrFrom returns a portion of string `str` starting from first occurrence of and including `need`
   193  // to the end of `str`.
   194  //
   195  // Example:
   196  // SubStrFrom("av.mp4", ".") -> ".mp4"
   197  func SubStrFrom(str string, need string) (substr string) {
   198  	pos := Pos(str, need)
   199  	if pos < 0 {
   200  		return ""
   201  	}
   202  	return str[pos:]
   203  }
   204  
   205  // SubStrFromEx returns a portion of string `str` starting from first occurrence of and excluding `need`
   206  // to the end of `str`.
   207  //
   208  // Example:
   209  // SubStrFromEx("av.mp4", ".") -> "mp4"
   210  func SubStrFromEx(str string, need string) (substr string) {
   211  	pos := Pos(str, need)
   212  	if pos < 0 {
   213  		return ""
   214  	}
   215  	return str[pos+len(need):]
   216  }
   217  
   218  // SubStrFromR returns a portion of string `str` starting from last occurrence of and including `need`
   219  // to the end of `str`.
   220  //
   221  // Example:
   222  // SubStrFromR("/dev/vda", "/") -> "/vda"
   223  func SubStrFromR(str string, need string) (substr string) {
   224  	pos := PosR(str, need)
   225  	if pos < 0 {
   226  		return ""
   227  	}
   228  	return str[pos:]
   229  }
   230  
   231  // SubStrFromREx returns a portion of string `str` starting from last occurrence of and excluding `need`
   232  // to the end of `str`.
   233  //
   234  // Example:
   235  // SubStrFromREx("/dev/vda", "/") -> "vda"
   236  func SubStrFromREx(str string, need string) (substr string) {
   237  	pos := PosR(str, need)
   238  	if pos < 0 {
   239  		return ""
   240  	}
   241  	return str[pos+len(need):]
   242  }