You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Contains checks if the string contains the specified substring and returns a boolean value. This is a wrapper around Go's standard strings.Contains function that fits into the Stringy interface.
159
+
160
+
```go
161
+
str:= stringy.New("Hello World")
162
+
fmt.Println(str.Contains("World")) // true
163
+
fmt.Println(str.Contains("Universe")) // false
164
+
```
165
+
141
166
#### ContainsAll(check ...string) bool
142
167
143
168
ContainsAll is variadic function which takes slice of strings as param and checks if they are present in input and returns boolean value accordingly.
@@ -173,6 +198,33 @@ First returns first n characters from provided input. It removes all spaces in s
173
198
174
199
Get simply returns result and can be chained on function which returns StringManipulation interface view above examples
175
200
201
+
```go
202
+
getString:= stringy.New("hello roshan")
203
+
fmt.Println(getString.Get()) // hello roshan
204
+
```
205
+
206
+
#### IsEmpty() bool
207
+
IsEmpty checks if the string is empty or contains only whitespace characters. It returns true for empty strings or strings containing only spaces, tabs, or newlines.
208
+
209
+
```go
210
+
emptyStr:= stringy.New("")
211
+
fmt.Println(emptyStr.IsEmpty()) // true
212
+
213
+
whitespaceStr:= stringy.New("\t\n")
214
+
fmt.Println(whitespaceStr.IsEmpty()) // true
215
+
216
+
normalStr:= stringy.New("Hello")
217
+
fmt.Println(normalStr.IsEmpty()) // false
218
+
219
+
emptyStr:= stringy.New("")
220
+
fmt.Println(emptyStr.IsEmpty()) // true
221
+
222
+
whitespaceStr:= stringy.New("\t\n")
223
+
fmt.Println(whitespaceStr.IsEmpty()) // true
224
+
225
+
normalStr:= stringy.New("Hello")
226
+
fmt.Println(normalStr.IsEmpty()) // false
227
+
176
228
177
229
#### KebabCase(rule ...string) StringManipulation
178
230
@@ -249,6 +301,15 @@ ReplaceFirst takes two param search and replace. It returns string by searching
249
301
fmt.Println(replaceFirst.ReplaceFirst("name", "nombre")) // Hello My nombre is Roshan and his name is Alis.
ReplaceAll replaces all occurrences of a search string with a replacement string. It complements the existing ReplaceFirst and ReplaceLast methods and provides a chainable wrapper around Go's strings.ReplaceAll function.
SentenceCase is a variadic function that takes one parameter: slice of strings named rule. It converts text from various formats (camelCase, snake_case, kebab-case, etc.) to sentence case format, where the first word is capitalized and the rest are lowercase, with words separated by spaces. Rule parameter helps to omit characters you want to omit from the string. By default, special characters like "_", "-", ".", " " are treated as word separators.
fmt.Println(mixedStr.SentenceCase("@", " ", "#", " ", "&", " ").Get()) // This is kebab and special chars
343
+
```
344
+
You can chain ToUpper which will make the result all uppercase or Get which will return the result as it is. The first word is automatically capitalized, and all other words are lowercase.
345
+
272
346
273
347
#### Shuffle() string
274
348
@@ -301,6 +375,19 @@ SnakeCase is variadic function that takes one Param slice of strings named rule
301
375
```
302
376
You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
303
377
378
+
#### Substring(start, end int) StringManipulation
379
+
Substring extracts part of a string from the start position (inclusive) to the end position (exclusive). It handles multi-byte characters correctly and has safety checks for out-of-bounds indices.
380
+
```go
381
+
// Basic usage
382
+
go str := stringy.New("Hello World")
383
+
fmt.Println(str.Substring(0, 5).Get()) // Hello
384
+
fmt.Println(str.Substring(6, 11).Get()) // World
385
+
386
+
// With multi-byte characters
387
+
str = stringy.New("Hello 世界")
388
+
fmt.Println(str.Substring(6, 8).Get()) // 世界
389
+
```
390
+
304
391
305
392
#### Tease(length int, indicator string) string
306
393
@@ -330,6 +417,20 @@ ToLower makes all string of user input to lowercase and it can be chained on fun
Trim removes leading and trailing whitespace or specified characters from the string. If no characters are specified, it trims whitespace by default. It can be chained with other methods that return StringManipulation interface.
422
+
```go
423
+
trimString := stringy.New(" Hello World ")
424
+
fmt.Println(trimString.Trim().Get()) // Hello World
425
+
426
+
specialTrim := stringy.New("!!!Hello World!!!")
427
+
fmt.Println(specialTrim.Trim("!").Get()) // Hello World
428
+
429
+
chainedTrim := stringy.New(" hello world ")
430
+
fmt.Println(chainedTrim.Trim().UcFirst()) // Hello world
431
+
```
432
+
You can chain ToUpper which will make the result all uppercase, ToLower which will make the result all lowercase, or Get which will return the result as it is.
433
+
333
434
334
435
#### ToUpper() string
335
436
@@ -373,6 +474,9 @@ Suffix makes sure string has been suffixed with a given string and avoids adding
373
474
374
475
#### Acronym() string
375
476
477
+
SlugifyWithCount(count int) StringManipulation
478
+
SlugifyWithCount creates a URL-friendly slug with an optional uniqueness counter appended. This is useful for creating unique URL slugs for blog posts, articles, or database entries.
479
+
376
480
Acronym func returns acronym of input string. You can chain ToUpper() which with make result all upercase or ToLower() which will make result all lower case or Get which will return result as it is
377
481
378
482
```go
@@ -395,9 +499,71 @@ look how it omitted ?## from string. If you dont want to omit anything and since
SlugifyWithCount creates a URL-friendly slug with an optional uniqueness counter appended. This is useful for creating unique URL slugs for blog posts, articles, or database entries.
TruncateWords truncates the string to a specified number of words and appends a suffix. This is useful for creating previews or summaries of longer text.
512
+
513
+
```go
514
+
truncate := stringy.New("This is a long sentence that needs to be truncated.")
515
+
fmt.Println(truncate.TruncateWords(5, "...").Get()) // This is a long sentence...
516
+
fmt.Println(truncate.TruncateWords(3, "...").ToUpper()) // THIS IS A LONG...
517
+
```
518
+
519
+
#### WordCount() int
520
+
WordCount returns the number of words in the string. It uses whitespace as the word separator and can be chained with other methods.
521
+
522
+
```go
523
+
wordCount := stringy.New("Hello World")
524
+
fmt.Println(wordCount.WordCount()) // 2
525
+
526
+
multiByteCount := stringy.New("Hello 世界")
527
+
fmt.Println(multiByteCount.WordCount()) // 2
528
+
```
529
+
530
+
#### Substring(start, end int) StringManipulation
531
+
Substring extracts part of a string from the start position (inclusive) to the end position (exclusive). It handles multi-byte characters correctly and has safety checks for out-of-bounds indices.
400
532
533
+
```go
534
+
// Basic usage
535
+
str := stringy.New("Hello World")
536
+
fmt.Println(str.Substring(0, 5).Get()) // Hello
537
+
fmt.Println(str.Substring(6, 11).Get()) // World
538
+
539
+
// With multi-byte characters
540
+
str = stringy.New("Hello 世界")
541
+
fmt.Println(str.Substring(6, 8).Get()) // 世界
542
+
```
543
+
544
+
#### Contains(substring string) bool
545
+
Contains checks if the string contains the specified substring and returns a boolean value. This is a wrapper around Go's standard strings.Contains function that fits into the Stringyinterface.
ReplaceAll replaces all occurrences of a search string with a replacement string. It complements the existing ReplaceFirst and ReplaceLast methods and provides a chainable wrapper around Go's strings.ReplaceAll function.
0 commit comments