Golang Strings

Explore the strings package. The methods in strings create, test and modify string data.

Strings. Text data is everywhere. It is in files, user input, on websites. A language's ability to create and manipulate strings is critical. This is a common task.

Func list. Here are some strings funcs. When possible, it is best to use methods that have been implemented in the standard library—these are well-tested.ContainsContainsAnyEqualFoldFieldsFieldsFuncHasPrefixHasSuffixIndexIndexAnyIndexFuncJoinLastIndexMapRepeatReplaceSplitSplitAfterSplitNTitleToLowerToUpperTrimTrimFuncTrimSpace

Literals. We can specify string literals in Golang with 2 different syntax forms. Raw string literals (with tick chars) should be used if escaped chars need to be encoded.String Literal

Substring, slices. There is no Substring() method to take ranges of characters in Go. Instead we use the slice syntax (with first and last indexes).Substring
Rune slice: For substrings, we often want to use rune slices. It is easy to convert a string into a rune slice in Golang.
Convert String, Rune Slice

Ciphers. We can translate strings based on characters in Go. We apply the strings.Map method to implement ciphers like ROT13, which rotates characters.
Caesar: This cipher shifts characters by a certain number of characters. It has historical significance.
Caesar Cipher
ROT13: This cipher rotates characters by 13 places (which explains the name). It is sometimes used to make text harder to read.

Reverse. To reverse a string, we must access it at the level of runes or bytes. Reversing a string helps us learn how to manipulate the contents of strings.Reverse String

String usage. In programming strings are everywhere. We use them constantly. With the powerful and complete set of funcs in the strings package, we can use them with ease.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.