
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
Go - Strings
Strings, which are widely used in Go programming, are a readonly slice of bytes. In the Go programming language, strings are slices. The Go platform provides various libraries to manipulate strings.
- unicode
- regexp
- strings
Creating Strings
The most direct way to create a string is to write −
var greeting = "Hello world!"
Whenever it encounters a string literal in your code, the compiler creates a string object with its value in this case, "Hello world!'.
A string literal holds a valid UTF-8 sequences called runes. A String holds arbitrary bytes.
Example
package main import "fmt" func main() { var greeting = "Hello world!" fmt.Printf("normal string: ") fmt.Printf("%s", greeting) fmt.Printf("\n") fmt.Printf("hex bytes: ") for i := 0; i < len(greeting); i++ { fmt.Printf("%x ", greeting[i]) } fmt.Printf("\n") const sampleText = "\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98" /*q flag escapes unprintable characters, with + flag it escapses non-ascii characters as well to make output unambigous */ fmt.Printf("quoted string: ") fmt.Printf("%+q", sampleText) fmt.Printf("\n") }
This would produce the following result −
normal string: Hello world! hex bytes: 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 quoted string: "\xbd\xb2=\xbc \u2318"
Note − The string literal is immutable, so that once it is created a string literal cannot be changed.
Finding String Length
The len(str)
method returns the number of bytes contained in the string literal.
Example
package main import "fmt" func main() { var greeting = "Hello world!" fmt.Printf("String Length is: ") fmt.Println(len(greeting)) }
This would produce the following result −
String Length is : 12
Concatenating Strings
The strings package includes a method join for concatenating multiple strings −
strings.Join(sample, " ")
Join concatenates the elements of an array to create a single string. Second parameter is seperator which is placed between element of the array.
Example
Let us look at the following example −
package main import ("fmt" "math" )"fmt" "strings") func main() { greetings := []string{"Hello","world!"} fmt.Println(strings.Join(greetings, " ")) }
This would produce the following result −
Hello world!
Iterate Over String Characters
You can iterate through each character of a string using a for
loop with the range
keyword. By using this, you can access both the index and the character.
Example
The following example demonstrates how you can iterate over the string characters:
package main import "fmt" func main() { str := "TutorialsPoint" for index, char := range str { fmt.Printf("Index: %d, Character: %c\n", index, char) } }
This would produce the following result −
Index: 0, Character: T Index: 1, Character: u Index: 2, Character: t Index: 3, Character: o Index: 4, Character: r Index: 5, Character: i Index: 6, Character: a Index: 7, Character: l Index: 8, Character: s Index: 9, Character: P Index: 10, Character: o Index: 11, Character: i Index: 12, Character: n Index: 13, Character: t