How to index characters in a Golang string?

StringGoCharacter

String Problem Overview


How to get an "E" output rather than 69?

package main

import "fmt"

func main() {
    fmt.Print("HELLO"[1])
}

Does Golang have function to convert a char to byte and vice versa?

String Solutions


Solution 1 - String

Interpreted string literals are character sequences between double quotes "" using the (possibly multi-byte) UTF-8 encoding of individual characters. In UTF-8, ASCII characters are single-byte corresponding to the first 128 Unicode characters. Strings behave like slices of bytes. A rune is an integer value identifying a Unicode code point. Therefore,

package main

import "fmt"

func main() {
	fmt.Println(string("Hello"[1]))              // ASCII only
	fmt.Println(string([]rune("Hello, 世界")[1])) // UTF-8
	fmt.Println(string([]rune("Hello, 世界")[8])) // UTF-8
}

Output:

e
e
界

Read:

Go Programming Language Specification section on Conversions.

The Go Blog: Strings, bytes, runes and characters in Go

Solution 2 - String

How about this?

fmt.Printf("%c","HELLO"[1])

As Peter points out, to allow for more than just ASCII:

fmt.Printf("%c", []rune("HELLO")[1])

Solution 3 - String

Can be done via slicing too

package main

import "fmt"

func main() {
    fmt.Print("HELLO"[1:2])
}

NOTE: This solution only works for ASCII characters.

Solution 4 - String

You can also try typecasting it with string.

package main

import "fmt"

func main() {
	fmt.Println(string("Hello"[1]))
}

Solution 5 - String

Go doesn't really have a character type as such. byte is often used for ASCII characters, and rune is used for Unicode characters, but they are both just aliases for integer types (uint8 and int32). So if you want to force them to be printed as characters instead of numbers, you need to use Printf("%c", x). The %c format specification works for any integer type.

Solution 6 - String

The general solution to interpreting a char as a string is string("HELLO"[1]).

Rich's solution also works, of course.

Solution 7 - String

Try this to get the charecters by their index

package main

import (
      "fmt"
      "strings"
)

func main() {
   str := strings.Split("HELLO","")
    fmt.Print(str[1])
}

Solution 8 - String

String characters are runes, so to print them, you have to turn them back into String.

fmt.Print(string("HELLO"[1]))

Solution 9 - String

Another Solution to isolate a character in a string

package main
import "fmt"

   func main() {
        var word string = "ZbjTS"

       // P R I N T 
       fmt.Println(word)
       yo := string([]rune(word)[0])
       fmt.Println(yo)

       //I N D E X 
       x :=0
       for x < len(word){
           yo := string([]rune(word)[x])
           fmt.Println(yo)
           x+=1
       }

}

for string arrays also:

fmt.Println(string([]rune(sArray[0])[0]))

// = commented line

Solution 10 - String

The solution will be :

 package main

 import "fmt"

func main() {
  str := "HELLO"
  string(str[0])//H
  string(str[1])//E
  string(str[2])//L
  string(str[3])//L
  string(str[4])//O
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser977828View Question on Stackoverflow
Solution 1 - StringpeterSOView Answer on Stackoverflow
Solution 2 - StringRich ChurcherView Answer on Stackoverflow
Solution 3 - StringSamkit JainView Answer on Stackoverflow
Solution 4 - StringinfiniteLearnerView Answer on Stackoverflow
Solution 5 - StringandybalholmView Answer on Stackoverflow
Solution 6 - StringThomas KapplerView Answer on Stackoverflow
Solution 7 - StringAnshuView Answer on Stackoverflow
Solution 8 - StringlemmerView Answer on Stackoverflow
Solution 9 - StringH...View Answer on Stackoverflow
Solution 10 - StringLogrhythm EngineeringView Answer on Stackoverflow