码迷,mamicode.com
首页 > 其他好文 > 详细

2.9 字母大小写

时间:2018-03-22 00:30:31      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:rap   source   ==   post   email   blog   diff   ons   mat   


package main

import (
    "fmt"
    "strings"
    "unicode"
)

const email = "ExamPle@domain.com"
const name = "isaac newton"
const upc = "upc"
const i = "i"

const snakeCase = "first_name"

func main() {

    // For comparing the user input
    // sometimes it is better to
    // compare the input in a same
    // case.
    input := "Example@domain.com"
    input = strings.ToLower(input)
    emailToCompare := strings.ToLower(email)
    matches := input == emailToCompare
    fmt.Printf("Email matches: %t\n", matches)

    upcCode := strings.ToUpper(upc)
    fmt.Println("UPPER case: " + upcCode)

    // This digraph has different upper case and
    // title case.
    str := "?"
    fmt.Printf("%s in upper: %s and title: %s \n",
        str,
        strings.ToUpper(str),
        strings.ToTitle(str))

    // Use of XXXSpecial function
    title := strings.ToTitle(i)
    titleTurk := strings.ToTitleSpecial(unicode.TurkishCase, i)
    if title != titleTurk {
        fmt.Printf("ToTitle is defferent: %#U vs. %#U \n",
            title[0],
            []rune(titleTurk)[0])
    }

    // In some cases the input
    // needs to be corrected in case.
    correctNameCase := strings.Title(name)
    fmt.Println("Corrected name: " + correctNameCase)

    // Converting the snake case
    // to camel case with use of
    // Title and ToLower functions.
    firstNameCamel := toCamelCase(snakeCase)
    fmt.Println("Camel case: " + firstNameCamel)

}

func toCamelCase(input string) string {
    titleSpace := strings.Title(strings.Replace(input, "_", " ", -1))
    camel := strings.Replace(titleSpace, " ", "", -1)
    return strings.ToLower(camel[:1]) + camel[1:]
}


/*
Email matches: true
UPPER case: UPC
? in upper: ? and title: ? 
ToTitle is defferent: U+0049 ‘I‘ vs. U+0130 ‘?‘ 
Corrected name: Isaac Newton
Camel case: firstName

*/

2.9 字母大小写

标签:rap   source   ==   post   email   blog   diff   ons   mat   

原文地址:https://www.cnblogs.com/zrdpy/p/8620678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!