10 examples of 'golang string split' in Go

Every line of 'golang string split' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
115func Strings_Split(Data string, Split string) []string {
116 return strings.Split(Data, Split)
117}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

149func split(str string) (words []string) {
150 repl := strings.NewReplacer(
151 "@", "At ",
152 "&", "And ",
153 "|", "Pipe ",
154 "$", "Dollar ",
155 "!", "Bang ",
156 "-", " ",
157 "_", " ",
158 )
159
160 rex1 := regexp.MustCompile(`(\p{Lu})`)
161 rex2 := regexp.MustCompile(`(\pL|\pM|\pN|\p{Pc})+`)
162
163 str = trim(str)
164
165 // Convert dash and underscore to spaces
166 str = repl.Replace(str)
167
168 // Split when uppercase is found (needed for Snake)
169 str = rex1.ReplaceAllString(str, " $1")
170
171 // check if consecutive single char things make up an initialism
172 once.Do(sortInitialisms)
173 for _, k := range initialisms {
174 str = strings.Replace(str, rex1.ReplaceAllString(k, " $1"), " "+k, -1)
175 }
176 // Get the final list of words
177 words = rex2.FindAllString(str, -1)
178
179 return
180}
46func split(s string) []string {
47 var sb strings.Builder
48 p := s[0]
49 sb.WriteByte(p)
50 for i := 1; i < len(s); i++ {
51 n := s[i]
52 if p != n {
53 sb.WriteByte('\n')
54 }
55 sb.WriteByte(n)
56 p = n
57 }
58 return strings.Split(sb.String(), "\n")
59}
321func splitStr(s string) (configDataArr []string) {
322 if common.IsWindows() {
323 configDataArr = strings.Split(s, "\r\n")
324 }
325 if len(configDataArr) < 3 {
326 configDataArr = strings.Split(s, "\n")
327 }
328 return
329}
190func splitString(_ RideEnvironment, args ...rideType) (rideType, error) {
191 s1, s2, err := twoStringsArgs(args)
192 if err != nil {
193 return nil, errors.Wrap(err, "splitString")
194 }
195 r := make(rideList, 0)
196 for _, p := range strings.Split(s1, s2) {
197 r = append(r, rideString(p))
198 }
199 return r, nil
200}
258func split2(p []byte, sep byte) (p1, p2 []byte) {
259 i := bytes.IndexByte(p, sep)
260 if i == -1 {
261 return p, nil
262 }
263 return p[:i], p[i+1:]
264}
103func split(s, c string) (string, string) { // nolint:gocritic // unnamedResult
104 i := strings.Index(s, c)
105 return s[:i], s[i+len(c):]
106}
179func split(str string) []string {
180 // check if consecutive single char things make up an initialism
181 once.Do(func() {
182 splitRex1 = regexp.MustCompile(`(\p{Lu})`)
183 splitRex2 = regexp.MustCompile(`(\pL|\pM|\pN|\p{Pc})+`)
184 splitReplacer = strings.NewReplacer(
185 "@", "At ",
186 "&", "And ",
187 "|", "Pipe ",
188 "$", "Dollar ",
189 "!", "Bang ",
190 "-", " ",
191 "_", " ",
192 )
193 ensureSorted()
194 })
195
196 str = trim(str)
197
198 // Convert dash and underscore to spaces
199 str = splitReplacer.Replace(str)
200
201 // Split when uppercase is found (needed for Snake)
202 str = splitRex1.ReplaceAllString(str, " $1")
203
204 for _, k := range initialisms {
205 str = strings.Replace(str, splitRex1.ReplaceAllString(k, " $1"), " "+k, -1)
206 }
207 // Get the final list of words
208 //words = rex2.FindAllString(str, -1)
209 return splitRex2.FindAllString(str, -1)
210}
124func Split(delimiter, source interface{}) string {
125 return encode(fmt.Sprintf(`{ "Fn::Split" : [ "%v", "%v" ] }`, delimiter, source))
126}
55func splitString(s string, set string) []string {
56 if len(set) == 1 {
57 return strings.Split(s, set)
58 }
59
60 slist := []string{}
61 charSet := map[rune]bool{}
62
63 for _, r := range set {
64 charSet[r] = true
65 }
66
67 start := 0
68 for i, r := range s {
69 if _, found := charSet[r]; found {
70 slist = append(slist, s[start:i])
71 start = i + utf8.RuneLen(r)
72 }
73 }
74 if start < len(s) {
75 slist = append(slist, s[start:])
76 }
77
78 return slist
79}

Related snippets