[Golang] Test if a String Consists of Single Letter or Character


Test if a string consists of single character or letter in Golang.

Run code on Go Playground

package main

import (
      "fmt"
      "strings"
)

func IsConsistOfSingleCharacter(s, c string) bool {
      return strings.TrimLeft(s, c) == ""
}

func main() {
      fmt.Println(IsConsistOfSingleCharacter("#####", "#"))
      fmt.Println(IsConsistOfSingleCharacter("aabaa", "a"))
      fmt.Println(IsConsistOfSingleCharacter("哈哈哈哈", "哈"))
      fmt.Println(IsConsistOfSingleCharacter("哈哈嘻哈哈", "哈"))
}

Tested on: Ubuntu Linux 16.10, Go 1.7.4 & Go Playground.


References:

[1]func TrimLeft - strings - The Go Programming Language