Strings and Runes

2024-02-12 22:18
文章标签 strings runes

本文主要是介绍Strings and Runes,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

字符串方法

  • 字符串是否包含子字符串
fmt.Println("Contains:", strings.Contains("test", "es"))

Contains: true

  • 字符串包含子字符串数量
fmt.Println("Count:", strings.Count("test", "t"))

Count: 2

  • 字符串是否包含前缀
fmt.Println("HasPrefix:", strings.HasPrefix("test", "te"))

HasPrefix: true

  • 字符串是否包含后缀
fmt.Println("HasSuffix:", strings.HasSuffix("test", "st"))

HasSuffix: true

  • 字符串中子字符串的位置
fmt.Println("Index:", strings.Index("test", "e"))

Index: 1

  • 字符串拼接
fmt.Println("Join:", strings.Join([]string{"a", "b"}, "-"))

Join: a-b

  • 字符串重复
fmt.Println("Repeat:", strings.Repeat("a", 5))

Repeat: aaaaa

  • 字符串替换子字符串
fmt.Println("Replace:", strings.Replace("foo", "o", "0", -1))
fmt.Println("Replace:", strings.Replace("foo", "o", "0", 1))

Replace: f00
Replace: f0o

第4个参数表示替换的数量。参数小于0,即没有数量限制,否则,仅替换指定数量的子字符串。

  • 字符串分割
fmt.Println("Split:", strings.Split("a-b-c-d-e", "-"))

Split: [a b c d e]

  • 字符串大小写转换
fmt.Println("ToLower:", strings.ToLower("TEST"))
fmt.Println("ToUpper:", strings.ToUpper("test"))

ToLower: test
ToUpper: TEST

字符串格式化

  • 格式化结构体
p := point{1, 2}
fmt.Printf("struct1: %v\n", p)
fmt.Printf("struct2: %+v\n", p)
fmt.Printf("struct3: %#v\n", p)

struct1: {1 2}
struct2: {x:1 y:2}
struct3: main.point{x:1, y:2}

  • 格式化数据类型
fmt.Printf("type: %T\n", "string")
fmt.Printf("type: %T\n", rune(10))
fmt.Printf("type: %T\n", byte(10))
fmt.Printf("type: %T\n", int64(10))
fmt.Printf("type: %T\n", float64(10))
fmt.Printf("type: %T\n", point{1, 2})

type: string
type: int32
type: uint8
type: int64
type: float64
type: main.point

  • 格式化基本类型数据
fmt.Printf("bool: %t\n", true)
fmt.Printf("int: %d\n", 123)
fmt.Printf("bin: %b\n", 14)
fmt.Printf("char: %c\n", 33)
fmt.Printf("hex: %x\n", 456)
fmt.Printf("float1: %f\n", 78.9)
fmt.Printf("float2: %e\n", 123400000.0)
fmt.Printf("float3: %E\n", 123400000.0)

bool: true
int: 123
bin: 1110
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08

  • 格式化字符串类型数据
fmt.Printf("str1: %s\n", "\"string\"")
fmt.Printf("str2: %q\n", "\"string\"")
fmt.Printf("str3: %v\n", "\"string\"")
fmt.Printf("str4: %x\n", "hex this")

str1: “string”
str2: ““string””
str3: “string”
str4: 6865782074686973

  • 格式化指针
p := point{1, 2}
fmt.Printf("pointer: %p\n", &p)

pointer: 0xc0000b2010

  • 格式化对齐
fmt.Printf("width1: |%6d|%6d|\n", 12, 345)
fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)
fmt.Printf("width3: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
fmt.Printf("width4: |%6s|%6s|\n", "foo", "b")
fmt.Printf("width5: |%-6s|%-6s|\n", "foo", "b")

width1: | 12| 345|
width2: | 1.20| 3.45|
width3: |1.20 |3.45 |
width4: | foo| b|
width5: |foo |b |

  • 格式化字符串获取
s := fmt.Sprintf("sprintf: a %s", "string")
fmt.Println(s)

sprintf: a string

  • 格式化字符串并写入命令行
fmt.Fprintf(os.Stderr, "io: an %s\n", "error")

io: an error

字符串长度

s := "学而不思则罔,思而不学则殆。"
fmt.Println(len(s))

42

fmt.Println(utf8.RuneCountInString(s))

14

获取字符串长度应使用utf8.RuneCountInString

字符串遍历

  1. 直接遍历
for i := 0; i < len(s); i++ {fmt.Printf("%x ", s[i])
}

e5 ad a6 e8 80 8c e4 b8 8d e6 80 9d e5 88 99 e7 bd 94 ef bc 8c e6 80 9d e8 80 8c e4 b8 8d e5 ad a6 e5 88 99 e6 ae 86 e3 80 82

  1. range迭代
for i, v := range s {fmt.Printf("%d -> %#U\n", i, v)
}

0 -> U+5B66 ‘学’
3 -> U+800C ‘而’
6 -> U+4E0D ‘不’
9 -> U+601D ‘思’
12 -> U+5219 ‘则’
15 -> U+7F54 ‘罔’
18 -> U+FF0C ‘,’
21 -> U+601D ‘思’
24 -> U+800C ‘而’
27 -> U+4E0D ‘不’
30 -> U+5B66 ‘学’
33 -> U+5219 ‘则’
36 -> U+6B86 ‘殆’
39 -> U+3002 ‘。’

  1. utf8.DecodeRuneInString获取字符宽度
for i := 0; i < len(s); {r, size := utf8.DecodeRuneInString(s[i:])fmt.Printf("%d -> %#U\n", i, r)i += size
}

0 -> U+5B66 ‘学’
3 -> U+800C ‘而’
6 -> U+4E0D ‘不’
9 -> U+601D ‘思’
12 -> U+5219 ‘则’
15 -> U+7F54 ‘罔’
18 -> U+FF0C ‘,’
21 -> U+601D ‘思’
24 -> U+800C ‘而’
27 -> U+4E0D ‘不’
30 -> U+5B66 ‘学’
33 -> U+5219 ‘则’
36 -> U+6B86 ‘殆’
39 -> U+3002 ‘。’

从以上运行结果得出结论,建议使用rangeutf8.DecodeRuneInString

字符比较

func runeEqual(r rune) {if r == '学' {fmt.Println("it is 学")} else if r == '思' {fmt.Println("it is 思")} else {fmt.Printf("it is not 学 or 思, it is %#U\n", r)}
}
for _, v := range s {runeEqual(v)
}

it is 学
it is not 学 or 思, it is U+800C ‘而’
it is not 学 or 思, it is U+4E0D ‘不’
it is 思
it is not 学 or 思, it is U+5219 ‘则’
it is not 学 or 思, it is U+7F54 ‘罔’
it is not 学 or 思, it is U+FF0C ‘,’
it is 思
it is not 学 or 思, it is U+800C ‘而’
it is not 学 or 思, it is U+4E0D ‘不’
it is 学
it is not 学 or 思, it is U+5219 ‘则’
it is not 学 or 思, it is U+6B86 ‘殆’
it is not 学 or 思, it is U+3002 ‘。’

这篇关于Strings and Runes的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/703744

相关文章

[LeetCode] 583. Delete Operation for Two Strings

题:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目 Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in

Swift 3.0 学习 -- 大写和小写字符串(Uppercase and Lowercase Strings)

在swift2.0的时候,您可以通过字符串的uppercaseString和lowercaseString属性来访问大写/小写版本的字符串。如下:

If an application has more than one locale, then all the strings declared in one language should als

字符串资源多国语言版本的出错问题 假如你仅仅针对国内语言 加上这句即可 //保留中文支持resConfigs "zh"

JavaScript - First step - Strings

var string = 'The revolution will not be televised.';var string = "The revolution will not be televised."; 转义字符 var bigmouth = 'I\'ve got no right to take my place...';bigmouth; 字符串连接 var one =

【Go】strings.Replace 与 bytes.Replace 调优

原文链接:https://blog.thinkeridea.com/201902/go/replcae_you_hua.html 标准库中函数大多数情况下更通用,性能并非最好的,还是不能过于迷信标准库,最近又有了新发现,strings.Replace 这个函数自身的效率已经很好了,但是在特定情况下效率并不是最好的,分享一下我如何优化的吧。 我的服务中有部分代码使用 strings.Replac

hdoj 2371 decoded string. Decode the Strings

http://acm.hdu.edu.cn/showproblem.php?pid=2371 题意:给出编码的原则,给一个字符串,输出该字符串经过m次解码后的字符串。 啊啊啊啊。。。。无耻的看错题意了,以为给出字符串输出经过m次解码的后的字符串,样例死活过不了,赛后才发现问的是“decoded string”. 即解码后的字符串。。 思路:对于 5 3 2 3 1 5 4 helol

C++学习,Strings

C ++支持的字符串的函数: No功能与目的1 strcpy(s1, s2); 将字符串s2复制到字符串s1中。 2 strcat(s1, s2); 将字符串s2连接到字符串s1的末尾。 3 strlen(s1); 返回字符串s1的长度。 4 strcmp(s1, s2); 如果s1和s2相同则返回0; 如果s1 如果s1> s2,则大于0。 5 strchr(s1, ch); 返回指向字符串s

leetcode 893. Groups of Special-Equivalent Strings

原题链接 You are given an array of strings of the same length words. In one move, you can swap any two even indexed characters or any two odd indexed characters of a string words[i]. Two strings words[

Power Strings(KMP算法)

题目描述 Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation

UVA 10679 I love Strings!!!(AC自动机)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1620 ProblemG ILove Strings!!! Input:Standard Input Output:Standard Output TimeLimit: 4 Sec