zoukankan      html  css  js  c++  java
  • Golang之字符串操作(反转中英文字符串)

    //字符串反转
    package main import
    "fmt" func reverse(str string) string { var result string strLen := len(str) for i := 0; i < strLen; i++ { result = result + fmt.Sprintf("%c", str[strLen-i-1]) } return result } func reverse1(str string) string { var result []byte tmp := []byte(str) length := len(str) for i := 0; i < length; i++ { result = append(result, tmp[length-i-1]) } return string(result) } func main() { var str1 = "hello" str2 := "world" str3 := fmt.Sprintf("%s %s", str1, str2) n := len(str3) fmt.Println(str3) fmt.Printf("len(str3)=%d ", n) substr := str3[0:5] fmt.Println(substr) substr = str3[6:] fmt.Println(substr) result := reverse(str3) fmt.Println(result) result = reverse1(result) fmt.Println(result) }

     字符串练习

    package main
    
    import (
        "fmt"
    )
    
    func testString() {
        var str = "hello"
        fmt.Printf("str[0]=%c len(str)=%d
    ", str[0], len(str))
    
        for index, val := range str {
            fmt.Printf("str[%d]=%c
    ", index, val)
        }
    
        //str[0] = '0'
        //fmt.Println("after modify:", str)
        var byteSlice []byte
        byteSlice = []byte(str)
        byteSlice[0] = '0'
        str = string(byteSlice)
    
        fmt.Println("after modify:", str)
    
        fmt.Printf("len(str)=%d
    ", len(str))
    
        str = "hello, 少林之巅"
        fmt.Printf("len(str)=%d
    ", len(str))
    
        str = "中问123"
        fmt.Printf("last:len(str)=%d
    ", len(str))
    
        var b rune = ''
        fmt.Printf("b=%c
    ", b)
    
        var runeSlice []rune
        runeSlice = []rune(str)
        fmt.Printf("str 长度:%d, len(str)=%d
    ", len(runeSlice), len(str))
    }
    
    func testReverseStringV1() {
        var str = "hello中文 "
        var bytes []byte = []byte(str)
    
        for i := 0; i < len(str)/2; i++ {
            tmp := bytes[len(str)-i-1]
            bytes[len(str)-i-1] = bytes[i]
            bytes[i] = tmp
        }
    
        str = string(bytes)
        fmt.Println(str)
    }
    
    func testReverseStringV2() {
        var str = "hello中文 "
        var r []rune = []rune(str)
    
        for i := 0; i < len(r)/2; i++ {
            tmp := r[len(r)-i-1]
            r[len(r)-i-1] = r[i]
            r[i] = tmp
        }
    
        str = string(r)
        fmt.Println(str)
    }
    
    func testHuiWen() {
        var str = "上海自来水来自海上"
        var r []rune = []rune(str)
    
        for i := 0; i < len(r)/2; i++ {
            tmp := r[len(r)-i-1]
            r[len(r)-i-1] = r[i]
            r[i] = tmp
        }
    
        str2 := string(r)
        if str2 == str {
            fmt.Println(str, " is huiwen")
        } else {
            fmt.Println(str, " is not huiwen")
        }
    }
    
    func main() {
        //testString()
        //testReverseStringV2()
        testHuiWen()
    }
  • 相关阅读:
    在am中定义消息集束,并在CO中验证之后抛出异常。
    在EORow或者VORow中对数据进行重复性校验
    axis2 webservice jar包使用情况(转)
    std::function以及std::bind
    Qt学习过程
    NULL和nullptr
    清空表且id为0
    C++线程互斥、同步
    warning: deleting 'void *' is undefined 错误
    Classification / Recognition
  • 原文地址:https://www.cnblogs.com/pyyu/p/8066088.html
Copyright © 2011-2022 走看看