github.com/gogf/gf/v2@v2.7.4/text/gstr/gstr_split_join.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  	"github.com/gogf/gf/v2/util/gconv"
    14  )
    15  
    16  // Split splits string `str` by a string `delimiter`, to an array.
    17  func Split(str, delimiter string) []string {
    18  	return strings.Split(str, delimiter)
    19  }
    20  
    21  // SplitAndTrim splits string `str` by a string `delimiter` to an array,
    22  // and calls Trim to every element of this array. It ignores the elements
    23  // which are empty after Trim.
    24  func SplitAndTrim(str, delimiter string, characterMask ...string) []string {
    25  	return utils.SplitAndTrim(str, delimiter, characterMask...)
    26  }
    27  
    28  // Join concatenates the elements of `array` to create a single string. The separator string
    29  // `sep` is placed between elements in the resulting string.
    30  func Join(array []string, sep string) string {
    31  	return strings.Join(array, sep)
    32  }
    33  
    34  // JoinAny concatenates the elements of `array` to create a single string. The separator string
    35  // `sep` is placed between elements in the resulting string.
    36  //
    37  // The parameter `array` can be any type of slice, which be converted to string array.
    38  func JoinAny(array interface{}, sep string) string {
    39  	return strings.Join(gconv.Strings(array), sep)
    40  }
    41  
    42  // Explode splits string `str` by a string `delimiter`, to an array.
    43  // See http://php.net/manual/en/function.explode.php.
    44  func Explode(delimiter, str string) []string {
    45  	return Split(str, delimiter)
    46  }
    47  
    48  // Implode joins array elements `pieces` with a string `glue`.
    49  // http://php.net/manual/en/function.implode.php
    50  func Implode(glue string, pieces []string) string {
    51  	return strings.Join(pieces, glue)
    52  }
    53  
    54  // ChunkSplit splits a string into smaller chunks.
    55  // Can be used to split a string into smaller chunks which is useful for
    56  // e.g. converting BASE64 string output to match RFC 2045 semantics.
    57  // It inserts end every chunkLen characters.
    58  // It considers parameter `body` and `end` as unicode string.
    59  func ChunkSplit(body string, chunkLen int, end string) string {
    60  	if end == "" {
    61  		end = "\r\n"
    62  	}
    63  	runes, endRunes := []rune(body), []rune(end)
    64  	l := len(runes)
    65  	if l <= 1 || l < chunkLen {
    66  		return body + end
    67  	}
    68  	ns := make([]rune, 0, len(runes)+len(endRunes))
    69  	for i := 0; i < l; i += chunkLen {
    70  		if i+chunkLen > l {
    71  			ns = append(ns, runes[i:]...)
    72  		} else {
    73  			ns = append(ns, runes[i:i+chunkLen]...)
    74  		}
    75  		ns = append(ns, endRunes...)
    76  	}
    77  	return string(ns)
    78  }
    79  
    80  // Fields returns the words used in a string as slice.
    81  func Fields(str string) []string {
    82  	return strings.Fields(str)
    83  }