HomeSearch

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.ContainsContainsAnyEqualFoldIndexIndexAnyIndexFuncJoinLastIndexMapRepeatSplitTitleToLowerToUpperTrimTrimFuncTrimSpace

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.

ROT13

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

In the strings package,

which we specify in an import block, we gain access to many funcs. Things like replacing string data, changing cases, and removing whitespace are easy.

Research.

Many string-based methods are implemented with the "strings" package. But often we can use rune slices to modify strings. This functionality is built into Go.

Quote: Package strings implements simple functions to manipulate strings.

Strings: golang.org

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.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.