Home  >  Article  >  Backend Development  >  How to implement uppercase and lowercase functions for the first letter of a string in Go

How to implement uppercase and lowercase functions for the first letter of a string in Go

藏色散人
藏色散人forward
2021-06-03 15:46:165770browse

The following tutorial column will introduce to you how to implement the uppercase and lowercase functions of the first letter of a string in Go. I hope it will be helpful to friends in need!

The Go language itself does not have

functions for uppercase or lowercase stringsHow to implement uppercase and lowercase functions for the first letter of a string in Go, only

strings.ToUpper(s string)

and strings.ToLower(s string) Can realize all uppercase and lowercase characters in strings respectively. On the basis of these two functions, encapsulation of the first letter of the string in uppercase and lowercase can be realized respectively:

// FirstUpper 字符串首字母大写
func FirstUpper(s string) string {
    if s == "" {
        return ""
    }
    return strings.ToUpper(s[:1]) + s[1:]
}

// FirstLower 字符串首字母小写
func FirstLower(s string) string {
  if s == "" {
      return ""
  }
  return strings.ToLower(s[:1]) + s[1:]
}

Related introduction:

Go (also known as Golang) is a statically strongly typed, compiled, concurrent programming language with garbage collection capabilities developed by Google.


Robert Griesemer, Rob Pike and Ken Thompson started designing Go in September 2007, and later Ian Lance Taylor, Russ Cox joins the project. Go is developed based on the Inferno operating system. Go was officially announced in November 2009, becoming an open source project and implemented on Linux and Mac OS X platforms, and later added implementation under Windows systems. In 2016, Go was selected as "TIOBE's Best Language of 2016" by the software evaluation company TIOBE. Currently, Go releases a second-level version every six months (that is, upgrading from a.x to a.y).

The above is the detailed content of How to implement uppercase and lowercase functions for the first letter of a string in Go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete