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

相关文章

android的strings整理脚本

统一对String整理的工具,结构如下 代码 package com.owant.toollib;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.ArrayList;import java.util.List;import java.util

[leetcode] 43. Multiply Strings(大数相乘)

Multiply Strings 描述 Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2. Note: 1. The length of both num1 and num2 is < 110. 2. Both num1 an

一个比官方strings.Title更精简高效的将字符串中所有单词首字母转换为大小写的go函数

在go语言的官方包 strings中,官方提供了Title函数用于将字符串中的单词首字母转换为大写,这个函数很绕,对于要转换的字符串先是一个Map循环,然后接着又是一个Map循环,且函数调函数掉了好多层,而且最新版本中已经标记为过时,推荐使用一个更绕的golang.org/x/text/cases包中的函数进行转换。 下面的函数使用了高效的正则来切割字符串,同时支持自定义切割字符来对字符串中的所

{ Cracking The Coding Interview: 150 programming QA } --- Arrays and Strings

Hashtable, ArrayList (Dynamically resizing array, providing O(1) access), StringBuffer (do string concatenation) 1. 判断string是否有重复字符,不允许使用其他数据结构。 Step 1: 问清楚限制,string的编码是ASCII还是Unicode a. 如果可以用其他数

linux 命令之strings

strings 命令是在对象文件或二进制文件中查找可打印的字符串。字符串是4个或者更多可打印字符串的任意序列,以换行符或者空字符结束。 strings语法 strings [options] file_name options: -a / -all : 扫描整个文件而不是只扫描目标文件初始化和装载段。 -f / -print-file-name:在显示字符串前显示文件名 -n / -b

Codeforces Round #226 (Div. 2) B. Bear and Strings

B. Bear and Strings time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output The bear has a string s = s1s2... s|s| (record |s|

Codeforces 385B Bear and Strings(字符串)

题目连接:Codeforces 385B Bear and Strings 题目大意:给出一个字符串,问说该字符串中有多少个子串包含“bear”。 解题思路:遍历,每次找到“bear”,就用该串前面个字符数x,以及该串后面的字符数y,ans += (x+1)*(y+1)- 前一个“bear”所在位置的字符串(重复的) #include <stdio.h>#includ

Leetcode 043 Multiply Strings(大数)

题目连接:Leetcode 043 Multiply Strings 解题思路:裸的大数乘法。 class Solution {public:string reverseString(string s) {int n = s.size();string ret = s;for (int i = 0; i < n; i++) ret[i] = s[n-i-1];return ret;}strin

golang中2个只定义不需要初始化的高效字符缓存类型 bytes.Buffer和strings.Builder使用示例

在golang中,有2个高效的用于字符数据写入的缓存类型,同时他们也都实现了io.Writer接口,一个是bytes包中的Buffer 这个还实现了io.Reader接口; 另外一个是 strings包中的字符串构建类型 Builder。 在使用他们的时候是不需要初始化的,只需要定义一个对应类型的变量即可,如: var buf bytes.Buffer;   bytes.Buffer使用示例

UVA 11081 Strings(dp)

string by combining two subsequences from the first two strings.             After deleting 0 or more characters from a string we can get its subsequence. For example “a”, “b”, “c”, “ab”, “ac”, “b