ADVERTISEMENT
ADVERTISEMENT

Golang strings Package Functions

In Go language, the strings package is used for various string-related operations, strings package has a variety of functions to manipulate UTF-8 encoded strings. To use these string functions – we need to import the strings package.

List of Golang strings Package Functions

Function Description
strings.Compare() It returns 0 if both strings are the same, -1 if the first string is less than the second string, and +1 if the first string is greater than the second string.
strings.Contains() It checks whether a substring is within the string. It returns true if the string contains the substring, false otherwise.
strings.ContainsRune() It checks whether a Unicode code point is within the string. It returns true if the string contains the Unicode code point, false otherwise.
strings.Count() It counts and returns the number of non-overlapping instances of substring in the string. If the substring is empty, it returns the number of Unicode code points in string +1.
strings.EqualFold() It checks whether the given strings (UTF-8 strings) are equal under Unicode case-folding (i.e., case-insensitive).
strings.Fields() It returns a slice of substrings if the string is there, otherwise, it returns an empty slice if the string contains only white spaces.
strings.FieldsFunc() It returns a slice of substrings if the function satisfies the condition applied on all code points of the string. If the string is empty, it returns an empty slice.
strings.HasPrefix() It returns true if the string begins with a prefix, false otherwise.
strings.HasSuffix() It returns true if the string ends with suffix, false otherwise.
strings.Index() It returns the index of the first occurrence (instance) of a substring in a string, or -1 if the substring is not present in the string.
strings.IndexAny() It returns the index of the first occurrence (instance) of any Unicode code point from substring in the string, or -1 if no Unicode code point from substring is present in the string.
strings.IndexByte() It gets the index of the first occurrence (instance) of a character (byte) in the string and returns the index, or -1 if the byte is not present in the string.
strings.IndexFunc() It gets the index of the first Unicode code point satisfying the function with respect to the character of the string. It returns the index, or -1 if any character of the string does not satisfy the function.
strings.IndexRune() It gets the index of the first occurrence (instance) of the Unicode code point (a rune type). It returns the index of the first instance of the character in the string, -1 otherwise.
strings.Join() It joins (concatenates) the slices of strings (elements of the first parameter) with the given separator to create a single string. It returns a concatenated string with the separator.
strings.LastIndex() It returns the index of the last occurrence (instance) of the substring in a string, or -1 if the substring is not present in the string.
strings.LastIndexAny() It returns the index of the last occurrence (instance) of any Unicode code point from substring in the string, or -1 if no Unicode code point from substring is present in the string.
strings.LastIndexByte() It returns the index of the last occurrence (instance) of character (byte) in the string, or -1 if the character (byte) is not present in the string.
strings.LastIndexFunc() It gets the index of the last Unicode code point satisfying the function with respect to the character of the string. It returns the index, or -1 if any character of the string does not satisfy the function.
strings.Map() It returns a copy of the given string with all its characters modified according to the mapping function.
strings.Repeat() It creates and returns a string consisting of count copies of the given string.
strings.Replace() It creates and returns a copy of the string with the first n non-overlapping occurrences (instances) of the old substring replaced by a new substring.
strings.Split() It splits the given string (slice) into all substrings separated by the given separator and returns a slice of the substrings between those separators.
strings.SplitAfter() It splits the given string (slice) into all substrings after each instance of the given separator and returns a slice of those substrings.
strings.SplitN() It splits the given string (slice) into all substrings by the given separator and returns a slice of the N substrings between those separators.
strings.Title() It returns a copy of the given string with all Unicode letters that begin words mapped to their Unicode title case.
strings.ToLower() It returns a copy of the string with all Unicode letters mapped to their lower case.
strings.ToLowerSpecial() It returns a copy of the string with all Unicode letters mapped to their lower case using the case mapping specified by unicode.SpecialCase type.
strings.ToUpper() It returns a copy of the string with all Unicode letters mapped to their upper case.
strings.ToUpperSpecial() It returns a copy of the string with all Unicode letters mapped to their upper case using the case mapping specified by unicode.SpecialCase type.
strings.ToValidUTF8() It returns a copy of the string with each run of invalid UTF-8 byte sequences replaced by the replacement string.
strings.Trim() It returns a string with all specified leading and trailing Unicode code points removed.
strings.TrimFunc() It returns a string with all leading and trailing Unicode code points c satisfying f(c) removed.
strings.TrimLeft() It returns a string with all specified leading Unicode code points removed.
strings.TrimLeftFunc() It returns a string with all leading Unicode code points c satisfying f(c) removed.
strings.TrimPrefix() It returns a string without the provided leading prefix string.
strings.TrimRight() It returns a string with all specified trailing Unicode code points removed.
strings.TrimRightFunc() It returns a string with all trailing Unicode code points c satisfying f(c) removed.
strings.TrimSpace() It returns a string with all leading and trailing white space removed, as defined by Unicode.
strings.TrimSuffix() It returns a string without the provided leading suffix string.

Example of strings.Compare()

// Golang program to demonstrate the
// example of strings.Compare() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Compare("abc", "abc"))
	fmt.Println(strings.Compare("abc", "abd"))
	fmt.Println(strings.Compare("xyz", "pqr"))
}

Output:

0
-1
1

Example of strings.Contains()

// Golang program to demonstrate the
// example of strings.Contains() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Contains("Hello, world!", "Hello"))
	fmt.Println(strings.Contains("Hello, world!", "world"))
	fmt.Println(strings.Contains("Hello, world!", "Hi"))
}

Output:

true
true
false

Example of strings.ContainsRune()

// Golang program to demonstrate the
// example of strings.ContainsRune() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	// 72 is the Unicode code point value for 'H' (Uppercase)
	// 119 is the Unicode code point value for 'w' (Lowercase)
	// 97 is the Unicode code point value for 'a' (Lowercase)

	fmt.Println(strings.ContainsRune("Hello, world!", 72))
	fmt.Println(strings.ContainsRune("Hello, world!", 119))
	fmt.Println(strings.ContainsRune("Hello, world!", 97))
}

Output:

true
true
false

Example strings.Count()

// Golang program to demonstrate the
// example of strings.Count() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.Count("Hello, world!", "o"))
	fmt.Println(strings.Count("Hello, world!", "l"))
	fmt.Println(strings.Count("Hello, world!", "Hi"))
	fmt.Println(strings.Count("Hi guys, she said Hi to everyone.", "Hi"))
	fmt.Println(strings.Count("Hello, world!", ""))
}

Output:

2
3
0
2
14

Example of strings.EqualFold()

// Golang program to demonstrate the
// example of strings.EqualFold() Function

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.EqualFold("Hello", "hello"))
	fmt.Println(strings.EqualFold("HELLO, WORLD!", "Hello, world!"))
	fmt.Println(strings.EqualFold("Hello There?", "Hi There?"))
}

Output:

true
true
false
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.