strings
CUE v0.18.0
Package strings implements simple functions to manipulate UTF-8 encoded strings.
Some of the functions in this package are specifically intended as field constraints. For instance, MaxRunes as used in this CUE program
import "strings" myString: strings.MaxRunes(5)specifies that the myString should be at most 5 code points.
Functions
ByteAt
ByteAt: func(b: bytes | string, i: int) -> intByteAt reports the ith byte of the underlying byte slice.
ByteSlice
ByteSlice: func(b: bytes | string, start: int, end: int) -> bytesByteSlice reports the bytes of the underlying byte slice from the start index up to but not including the end index.
Compare
Compare: func(a: string, b: string) -> intCompare returns an integer comparing two strings lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
Compare is included only for symmetry with package bytes. It is usually clearer and always faster to use the built-in string comparison operators ==, <, >, and so on.
Contains
Contains: func(s: string, substr: string) -> boolContains reports whether substr is within s.
ContainsAny
ContainsAny: func(s: string, chars: string) -> boolContainsAny reports whether any Unicode code points in chars are within s.
Count
Count: func(s: string, substr: string) -> intCount counts the number of non-overlapping instances of substr in s. If substr is an empty string, Count returns 1 + the number of Unicode code points in s.
Fields
Fields: func(s: string) -> [...string]Fields splits the string s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of s or an empty slice if s contains only white space.
HasPrefix
HasPrefix: func(s: string, prefix: string) -> boolHasPrefix tests whether the string s begins with prefix.
HasSuffix
HasSuffix: func(s: string, suffix: string) -> boolHasSuffix tests whether the string s ends with suffix.
Index
Index: func(s: string, substr: string) -> intIndex returns the index of the first instance of substr in s, or -1 if substr is not present in s.
IndexAny
IndexAny: func(s: string, chars: string) -> intIndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
Join
Join: func(elems: [...string], sep: string) -> stringJoin concatenates the elements of its first argument to create a single string. The separator string sep is placed between elements in the resulting string.
LastIndex
LastIndex: func(s: string, substr: string) -> intLastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
LastIndexAny
LastIndexAny: func(s: string, chars: string) -> intLastIndexAny returns the index of the last instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
MaxRunes validator
validator form
func(max: int) -> validator(string)call form
func(s: string, max: int) -> boolMaxRunes reports whether the number of runes (Unicode codepoints) in a string exceeds a certain maximum. MaxRunes can be used as a field constraint to accept all strings for which this property holds.
MinRunes validator
validator form
func(min: int) -> validator(string)call form
func(s: string, min: int) -> boolMinRunes reports whether the number of runes (Unicode codepoints) in a string is at least a certain minimum. MinRunes can be used as a field constraint to accept all strings for which this property holds.
Repeat
Repeat: func(s: string, count: int) -> stringRepeat returns a new string consisting of count copies of the string s.
Replace
Replace: func(s: string, old: string, new: string, n: int) -> stringReplace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the string and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune string. If n < 0, there is no limit on the number of replacements.
Runes
Runes: func(s: string) -> [...]Runes returns the Unicode code points of the given string.
SliceRunes
SliceRunes: func(s: string, start: int, end: int) -> stringSliceRunes returns a string of the underlying string data from the start index up to but not including the end index.
Split
Split: func(s: string, sep: string) -> [...string]Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators.
If s does not contain sep and sep is not empty, Split returns a slice of length 1 whose only element is s.
If sep is empty, Split splits after each UTF-8 sequence. If both s and sep are empty, Split returns an empty slice.
It is equivalent to SplitN with a count of -1.
SplitAfter
SplitAfter: func(s: string, sep: string) -> [...string]SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings.
If s does not contain sep and sep is not empty, SplitAfter returns a slice of length 1 whose only element is s.
If sep is empty, SplitAfter splits after each UTF-8 sequence. If both s and sep are empty, SplitAfter returns an empty slice.
It is equivalent to SplitAfterN with a count of -1.
SplitAfterN
SplitAfterN: func(s: string, sep: string, n: int) -> [...string]SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings.
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
n < 0: all substringsEdge cases for s and sep (for example, empty strings) are handled as described in the documentation for SplitAfter.
SplitN
SplitN: func(s: string, sep: string, n: int) -> [...string]SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators.
The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder.
n == 0: the result is nil (zero substrings)
n < 0: all substringsEdge cases for s and sep (for example, empty strings) are handled as described in the documentation for Split.
ToCamel
ToCamel: func(s: string) -> stringToCamel returns a copy of the string s with all Unicode letters that begin words mapped to lower case.
ToLower
ToLower: func(s: string) -> stringToLower returns s with all Unicode letters mapped to their lower case.
ToTitle
ToTitle: func(s: string) -> stringToTitle returns a copy of the string s with all Unicode letters that begin words mapped to their title case.
ToUpper
ToUpper: func(s: string) -> stringToUpper returns s with all Unicode letters mapped to their upper case.
Trim
Trim: func(s: string, cutset: string) -> stringTrim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
TrimLeft
TrimLeft: func(s: string, cutset: string) -> stringTrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
To remove a prefix, use [TrimPrefix] instead.
TrimPrefix
TrimPrefix: func(s: string, prefix: string) -> stringTrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.
TrimRight
TrimRight: func(s: string, cutset: string) -> stringTrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.
To remove a suffix, use [TrimSuffix] instead.
TrimSpace
TrimSpace: func(s: string) -> stringTrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.
TrimSuffix
TrimSuffix: func(s: string, suffix: string) -> stringTrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.