Strings in Golang
Overview
Strings in the go language are different from that of C++, Java, and Python. It is a sequence of characters. The bytes of characters are represented by UTF-8 encoding in string. In this article, we will be covering all about strings in the go language.
Introduction
The string is a slice of bytes enclosed in “ “. It can be created by enclosing contents in “ “. Strings in the go language are immutable which means once a string is created it’s not possible to change it.
A Simple Example of a Golang String
Example of String in Go
Go program to illustrate how to create strings
Output:
Explanation: In this example, the variables value_1 and value_2 are initialized with two different String values. These variables store the respective values of the string. Both values are printed at the output.
Golang String using Backtick
In this, the string literals are created using ``` also known as raw literals. They do not support characters like an escape, can scan multiple lines, and can contain any character except backtick (``). In the go language, it is generally used for writing a message that contains multiple lines, both in regular expression as well as in HTML. Example of string using backtick: Go program to illustrate string literal
Output:
Explanation: In this example, all the string values are initialized differently. The value_4 is where the backtick is used. In the value_4 however, the \n, i.e new line does not work because of backtick. The values are printed at the output as shown.
Access Characters of String in Go
- Index returns the byte value of the character.
- [s
] returns a substring from s to e-1. For example:
Output:
Explanation: In this example, the first line prints the ASCII code of the ‘a’, while the second line prints the string ‘a’.
Find the Length of a String
In Go lang, the length of the string can be found in two ways. The first way is to use the len() method and the second one is to useRuneCountInString() method. len(): it is used to return the number of elements in the array.
For example:
In order to use the RuneCountInString() method, the UTF-8 package needs to be imported.
Join Two Strings Together
Joining two strings also means concatenation. “+’ operator performs concatenation while “+=” also performs concatenation. Syntax:
Example of concatenation:
When the code is executed, the output is given as “Scaler Academy”. This is because s3 is assigned the concatenation of s1 and s2 values, here “+” in strings acts as joining an operator for joining/ concatenating two values of a string.
Golang String Methods
The string package in Go provides different methods which are useful for performing any string operation. Following is the list of methods that are widely used in Go lang:
Method | Description |
---|---|
Contains() | It is used to check if the substring is present inside a string. |
Compare() | It is used for comparing two different strings. |
Replaces() | It is used for replacing a substring with that of another substring. |
ToUpper() | It is used for covering a string to uppercase. |
ToLower() | It is used for converting a string to lowercase. |
Split() | It is used for splitting a string into multiple different substrings. |
Compare Two Strings in Go
String comparison EqualFold: it is used to compare the strings in a case-insensitive manner. Syntax:
Check if the String Contains a Substring
In Go lang, String Contains and ContainsAny are used for checking if the string contains a substring. Contains: it reports whether the substring is within s. ContainsAny: it reports whether any unicode points in char are in s.
Syntax:
Example:
Replace a String in Go
Replace: it returns non-overlapping instances of old replaced by new. If n is less than zero then there is no limit on the number of replacements which means it replaces every occurrence.
Examples:
Change the Case of the Go String
String data comparison ToUpper: it returns a copy of the string mapped to their upper case. ToLower: it returns a copy of the string mapped to their lowercase.
Examples:
Split Strings in Golang
Split: it returns substrings between the separators. Example:
Other String Operations
Compare Golang Strings using == The comparison of strings in Go lang performs lexograpical comparison(sort words alphabetically). It checks the Unicode of the alphabet/character in a string. The comparison done is case-sensitive. Examples:
Create Strings from a Slice
Slice: [s
Example:
Loop Through Strings in Golang
Loop through strings in golang means iterating through each character in the string. Example:
Output:
Explanation: In this example, the for loop iterates over through the string, and at each iteration it prints one character of the string.
Escape Sequence in Golang String
Escape sequences in Go lang is special characters that when used with string literals have a specific meaning. Example:
Output:-
Backslash(\) in Go lang works as an escape sequence character. For example, the (\n) which means the new line escape sequence makes the next character in the string to be printed on a new line.
Escape characters in Go lang:
Character | Description |
---|---|
\x | used for two hexadecimal digits |
\ | used for three octal digits |
\u | used for four hexadecimal digits |
\U | used for eight hexadecimal digits |
\a | Alert or bell |
\b | used for Backspace |
\ | Backslash |
\t | used for Horizontal tab |
\n | Line feed or newline |
\f | Form feed |
\r | Carriage return |
\v | Vertical tab |
' | used for Single quote |
" | Double quote |
Go Strings are Immutable
In Go lang, the strings are immutable just like any other programming language. This means the string cannot be changed or updated once it is initialized, instead a new string is created.
Conclusion
- String is a sequence of characters. The bytes of characters are represented by UTF-8 encoding in string.
- It can be created by enclosing contents in “ “. Strings in the go language are immutable which means once a string is created it’s not possible to change it.
- Methods in Golang:
- Contains()
- Compare()
- Replaces()
- ToUpper()
- ToLower()
- Split()
- Escape sequences in Go lang is special characters that when used with string literals have a specific meaning.