5

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

 2 years ago
source link: http://siongui.github.io/2017/01/17/go-test-if-string-consist-of-single-letter-or-character/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

January 17, 2017

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK