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

相关文章

Equalizing Two Strings 冒泡排序or树状数组

首先考虑排序后相等 如果排序后相等的话就只考虑reverse长度为2的,所以a或者b排序后存在相邻两个字母相等的话就puts YES,n>26也直接puts YES 不然的话就假设c为a,b排完序的排列 相当于判断经过多少次两两交换后a->c,b->c 如果两者变换次数差值为偶数就puts YES,不然puts NO 因为n>26的可以直接puts YES,所以把a,b分别进行冒泡排序求

android中strings.xml占位符

在strings.xml中我们存放需要使用的字符串。 但是有时候一些字符串的细节不能直接决定,需要之后考虑。 <string name="crime_report">%1$s!The crime was discovered on %2$s. %3$s, and %4$s</string>%1$s、%2$s等字符就是占位符,它们能够接收字符串。 java代码: getStrin

Oracle PL/SQL Programming 第8章:Strings 读书笔记

总的目录和进度,请参见开始读 Oracle PL/SQL Programming 第6版 具有字符数据类型的变量存储文本并由字符函数操作。 本章重点讨论单字节字符集,不涉及Unicode 或多字节字符集,不涉及CLOB(字符大对象)和 LONG。 String Datatypes Oracle 支持四种字符串数据类型: 定长变长数据库字符集CHARVARCHAR国家字符集NCHARNVA

583. Delete Operation for Two Strings (Medium) 最长公共子序列

公共子序列 public static int lengthOfLIS2(int[] nums1,int[] nums2) {int[][] dp=new int[nums1.length+1][nums2.length+1];for(int i=1;i<=nums1.length;i++){for(int j=1;j<=nums2.length;j++) {if (nums1[i-1]

Strings in the Pocket ZOJ - 4110

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4110 先看两个字符串是否相等  若相等则直接用马拉车求回文半径即可 否则 找出l和r来 满足[0,l-1]与[r+1,n-1]内两字符串相等 然后看两字符串[l,r]内是否完全相反 若不完全相反 即翻转后仍无法相等则输出0 否则就以(l+r)/2即不相等子串的中心 左右

strings.xml补充知识

复数名词 <plurals name="book"><item name="one">book</item><item name="others">books</item></plurals>int bookCount = 4;Resources res = getResources();String bookCount = res.getQuantityString(R.plurals.

leetcode:(205) Isomorphic Strings(java)

package LeetCode_HashTable;/*** 题目:* Given two strings s and t, determine if they are isomorphic.* Two strings are isomorphic if the characters in s can be replaced to get t.* All occur

[LeetCode]43.Multiply Strings

【题目】 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 【分析】 高精度乘法(大数乘法) 其实更多地是考察乘法运算

[Guava源码日报](2)Strings分析

我们先看大体看一下Strings的源码: //// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package com.google.common.base;import com.google.common.annotations.GwtCom

415. Add Strings(字符串相加)

问题描述 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和并同样以字符串形式返回。 你不能使用任何內建的用于处理大整数的库(比如 BigInteger), 也不能直接将输入的字符串转换为整数形式。 问题分析 按照数字相加以及进位法则写出代码就行。 代码 char* addStrings(char* num1, char* num2) {int length_num1