• 2.9 字母大小写


    
    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
    ", matches)
    
    	upcCode := strings.ToUpper(upc)
    	fmt.Println("UPPER case: " + upcCode)
    
    	// This digraph has different upper case and
    	// title case.
    	str := "dz"
    	fmt.Printf("%s in upper: %s and title: %s 
    ",
    		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 
    ",
    			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
    dz in upper: DZ and title: Dz 
    ToTitle is defferent: U+0049 'I' vs. U+0130 'İ' 
    Corrected name: Isaac Newton
    Camel case: firstName
    
    */
    
  • 相关阅读:
    学习进度博客六
    Ultimate四则运算
    水骑士团队介绍
    返回一个二维整数数组中最大联通子数组的和
    学习进度博客五
    构建之法阅读笔记02
    四则运算4
    敏捷开发方法综述
    第一冲刺阶段站立会议02
    学习进度表_七周
  • 原文地址:https://www.cnblogs.com/zrdpy/p/8620678.html
Copyright © 2020-2023  润新知