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

藏色散人
Release: 2021-06-03 15:46:16
forward
5775 people have browsed it

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:]
}
Copy after login

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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template