Golang: String as Chars

By Xah Lee. Date: . Last updated: .

Here's common solutions working with golang string as character sequence.

Number of Characters

To count the number of character, convert it rune slice, then call len

package main

import "fmt"

func main() {

	const x = "I β™₯ U"

	var y = []rune(x)

	fmt.Printf("%v\n", len(y)) // 5

}

Alternatively, use page

import "unicode/utf8"

utf8.RuneCountInString(string) β†’ returns the number of character in string. (character here means Unicode codepoint, aka rune)

package main

import "fmt"
import "unicode/utf8"

func main() {
    var x = "I β™₯ U"

    // number of bytes
    fmt.Printf("%v\n", len(x)) // 7

    // number of characters
    fmt.Printf("%v\n", utf8.RuneCountInString(x)) // 5
}

SubString of by Character Index

To get a substring with proper character boundaries, convert it to rune slice first. Like this:

package main

import "fmt"

func main() {

	// string of unicode
	var x = "β™₯πŸ˜‚β†’β˜…πŸŽ"

	// convert to rune slice
	var y = []rune(x)

	// take a slice from index 2 to 3
	var z = y[2:4]

	// print as chars
	fmt.Printf("%q\n", z) // ['β†’' 'β˜…']

	// print in go syntax
	fmt.Printf("%#v\n", z) // []int32{8594, 9733}

}

String Functions

String functions are in package β€œstrings”.

see https://golang.org/pkg/strings/

Here's some example.

package main

import "fmt"
import "strings"

var pl = fmt.Println

func main() {

    pl("ab" == "ab") // 0

    pl(strings.Contains("abcd", "bc"))  // true
    pl(strings.HasPrefix("abca", "ab")) // true
    pl(strings.HasSuffix("abca", "ca")) // true

    pl(strings.ToLower("ABC") == "abc") // true

    pl(strings.Trim(" abc ", " ") == "abc") // true

    pl(strings.Count("abcaab", "ab") == 2) // true

    pl(strings.Index("abc", "bc") == 1) // true

    pl(strings.Join([]string{"a", "and", "b"}, " ") == "a and b") // true

    // split into slice
    pl(strings.Split("a b c", " ")) // [a b c]
}

Find Replace

Use the regex package.

See: Golang: regexp

Loop Thru Character in String

for i, c := range string {…} β†’ go thru characters in string. i is the index (with respect to bytes), c is the character.

package main

import "fmt"

func main() {
    const x = "abcβ™₯ πŸ˜‚d"
    for i, c := range x {
        fmt.Printf("%v %q\n", i, c)
    }
}

// 0 'a'
// 1 'b'
// 2 'c'
// 3 'β™₯'
// 6 ' '
// 7 'πŸ˜‚'
// 11 'd'

if you don't need the index, do:

for _, c := range string {…}

package main

import "fmt"

func main() {
    const x = "β™₯ πŸ˜‚"
    for _, c := range x {
        fmt.Printf("%q, %U\n", c, c)
    }
}

// 'β™₯', U+2665
// ' ', U+0020
// 'πŸ˜‚', U+1F602

Note: when you loop thru string by range, each character in string is basically turned into a β€œrune” type, which is golang's term for Unicode codepoint. That is, a integer id for the character.

package main

import "fmt"

func main() {
    const x = "β™₯ πŸ˜‚"
    for _, c := range x {
        // print the char and its type
        fmt.Printf("%q, %T\n", c, c)
    }
}

// 'β™₯', int32
// ' ', int32
// 'πŸ˜‚', int32

[see Golang: Rune]

Embed Expression in String?

There is no embeding expression in string. (as in Ruby or JavaScript)

The closest is this:

fmt.Sprintf("Name: %v\nAge: %v", "John", 10)

package main

import "fmt"

func main() {

    var name = "John"
    var age = 30

    var x = fmt.Sprintf("Name: %v, Age: %v", name, age)

    fmt.Println(x) // Name: John, Age: 30
}

If you have a question, put $5 at patreon and message me.

Golang

  1. Compile, Run
  2. Source Encoding
  3. Package, Import
  4. Comment
  5. Print
  6. String
  7. Rune
  8. String as Chars
  9. Variable
  10. Zero Value
  11. Constant
  12. If Then Else
  13. Switch/Case
  14. Loop
  15. Basic Types
  16. Array
  17. Slice
  18. Map
  19. Struct
  20. Function
  21. regexp
  22. Read File
  23. Write to File
  24. Walk Dir
  25. Check File Exist
  26. System Call
  27. Get Script Path
  28. Pointer
  29. Defer
  30. Random Number

Examples

  1. match any regexp
  2. Validate Links
  3. Generate Sitemap

Reference

  1. Go Spec