首页 > 其他分享 >Golang基础-字符串

Golang基础-字符串

时间:2023-02-18 15:35:25浏览次数:43  
标签:Repeat func string 基础 Golang literal characters 字符串 strings

Strings

A string in Go is an immutable(不可变的) sequence of bytes, which don't necessarily have to represent characters.
double quotes("") 和 backticks(`)的区别:双引号里可能有特殊字符(Special characters),例如\t, \n等,不能换行。反引号,raw string literal,没有特殊字符,没有\代表转义这一说,可以换行。
Raw string literals use backticks (`) as their delimiter instead of double quotes and all characters in it are interpreted literally, meaning that there is no need to escape characters or newlines.

const daltons = `Joe
William
Jack
Averell`
`abc`
// same as "abc"

"\"" // regular string literal with 1 character: a double quote
// same as `"` a raw string literal with 1 character: a double quote

`\n
` // raw string literal with 3 character: a backslash, an 'n', and a newline
// same as "\\n\n"  a regular string literal with 3 characters: a backslash, an 'n', and a newline

"\t\n" // regular string literal with 2 characters: a tab and a newline
`\t\n`// raw string literal with 4 characters: two backslashes, a 't', and an 'n'

Special characters

Value Description
\a Alert or bell
\b Backspace
\\ Backslash
\t Horizontal tab
\n Line feed or newline
\f Form feed
\r Carriage return
\v Vertical tab
\` Single quote
\" Double quote

Strings Package

常用函数

func ToLower(s string) string
func ToUpper(s string) string

//Trim leading and trailing whitespace
func TrimSpace(s string) string

//Find the index of the first instance of a substring within a string
func Index(s, substr string) int

func Replace(s, old, new string, n int) string
func ReplaceAll(s, old, new string) string
func Split(s, sep string) []string

//测试字符串 s 是否以 suffix 结尾。
func HasSuffix(s, suffix string) bool

//Count the number of occurrences of a substring within a string
func Count(s, substr string) int

//Repeat returns a new string consisting of count copies of the string s.
//It panics if count is negative or if the result of (len(s) * count) overflows.
func Repeat(s string, count int) string
// strings.Repeat returns a string with a substring given as argument repeated many times
strings.Repeat("Go", 3)
// => "GoGoGo"

Exercise

package techpalace
import "strings"


// WelcomeMessage returns a welcome message for the customer.
func WelcomeMessage(customer string) string {
    return "Welcome to the Tech Palace, " + strings.ToUpper(customer)
}

// AddBorder adds a border to a welcome message.
func AddBorder(welcomeMsg string, numStarsPerLine int) string {
    return strings.Repeat("*", numStarsPerLine) + "\n" + welcomeMsg + "\n" + strings.Repeat("*", numStarsPerLine)
}

// CleanupMessage cleans up an old marketing message.
func CleanupMessage(oldMsg string) string {
    res := strings.ReplaceAll(oldMsg, "*", "")
    res = strings.TrimSpace(res)
    return res
}

标签:Repeat,func,string,基础,Golang,literal,characters,字符串,strings
From: https://www.cnblogs.com/roadwide/p/17132718.html

相关文章

  • 【Java-01】java基础-基本语法
    1、基本输出语句/**java*多行注释*///java单行注释publicclass_01_HelloWorld{publicstaticvoidmain(String[]args){//main方法System.......
  • python Django基础
    django官网https://www.djangoproject.com/download/文档https://docs.djangoproject.com/安装Django安装官网LTS版本pipinstalldjango==3.2.15Django命令>django......
  • Arthas基础学习
    《Arthas基础学习》概述Arthas(阿尔萨斯)能为你做什么?Arthas是Alibaba开源的Java诊断工具,深受开发者喜爱。当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:......
  • Java基础知识点(方法)
    1.方法是程序中最小的执行单元。2.作用:能够提高代码的复用性,提高代码的可维护性(好处)重复代码、具有独立功能的代码可以抽取到方法中。3.方法的定义:把一些代码打包在一起。方......
  • VIM基础配置
    VIM基础配置参考资料VimrcConfigurationGuide-HowtoCustomizeYourVimCodeEditorwithMappings,Vimscript,StatusLine,andMorehttps://github.com/pres......
  • Golang基础-Basics
    PackagesGo语言中的包和其他语言的库或模块的概念类似,目的都是为了支持模块化、封装、单独编译和代码重用。一个包的源代码保存在一个或多个以.go为文件后缀名的源文件中,......
  • 事务基础知识与执行计划
    事务基础知识数据库事务的概念数据库事务是什么?事务是一组原子性的SQL操作。事务由事务开始与事务结束之间执行的全部数据库操作组成。A(原子性)、(C一致性)、I(隔离性)、D(持......
  • golang流程控制if,switch分支
    if分支if单分支if条件表达式{逻辑代码}packagemainimport"fmt"funcmain(){ //varaint=9 //ifa<10{//判断a《10位true,所以为执行下面的打印a的......
  • Edgio赞助OWASP ModSecurity CRS,进一步推动以OWASP核心规则集为基础的高级应用安全发
    亚利桑那州凤凰城,2023年2月2日—EdgioInc.(纳斯达克:EGIO),作为以速度、安全性和易用性著称的首选平台,Edgio今天宣布成为开放网络应用安全项目(OWASP)下,ModSecurity核心规则集......
  • 调用自定义的SplitString函数对字符串进行分割
    voidSplitString(conststd::string&s,std::vector<std::string>&v,conststd::string&c){ std::string::size_typepos1,pos2; pos2=s.find(c); pos1=0; while......