github.com/wangyougui/gf/v2@v2.6.5/text/gstr/gstr_list.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/wangyougui/gf.
     6  
     7  package gstr
     8  
     9  // List2 Split the `str` with `delimiter` and returns the result as two parts string.
    10  func List2(str, delimiter string) (part1, part2 string) {
    11  	return doList2(delimiter, Split(str, delimiter))
    12  }
    13  
    14  // ListAndTrim2 SplitAndTrim the `str` with `delimiter` and returns the result as two parts string.
    15  func ListAndTrim2(str, delimiter string) (part1, part2 string) {
    16  	return doList2(delimiter, SplitAndTrim(str, delimiter))
    17  }
    18  
    19  func doList2(delimiter string, array []string) (part1, part2 string) {
    20  	switch len(array) {
    21  	case 0:
    22  		return "", ""
    23  	case 1:
    24  		return array[0], ""
    25  	case 2:
    26  		return array[0], array[1]
    27  	default:
    28  		return array[0], Join(array[1:], delimiter)
    29  	}
    30  }
    31  
    32  // List3 Split the `str` with `delimiter` and returns the result as three parts string.
    33  func List3(str, delimiter string) (part1, part2, part3 string) {
    34  	return doList3(delimiter, Split(str, delimiter))
    35  }
    36  
    37  // ListAndTrim3 SplitAndTrim the `str` with `delimiter` and returns the result as three parts string.
    38  func ListAndTrim3(str, delimiter string) (part1, part2, part3 string) {
    39  	return doList3(delimiter, SplitAndTrim(str, delimiter))
    40  }
    41  
    42  func doList3(delimiter string, array []string) (part1, part2, part3 string) {
    43  	switch len(array) {
    44  	case 0:
    45  		return "", "", ""
    46  	case 1:
    47  		return array[0], "", ""
    48  	case 2:
    49  		return array[0], array[1], ""
    50  	case 3:
    51  		return array[0], array[1], array[2]
    52  	default:
    53  		return array[0], array[1], Join(array[2:], delimiter)
    54  	}
    55  }