当前位置: 首页 > news >正文

谷歌做网站网站流量排名查询工具

谷歌做网站,网站流量排名查询工具,丰台建设公司网站,装修公司加盟十大品牌排行榜本文介绍go语言处理字符串类型的常见函数。 ## 多行字符串 在 Go 中创建多行字符串非常容易。只需要在你声明或赋值时使用 () 。 str : This is a multiline string. ## 字符串的拼接 go // fmt.Sprintf方式拼接字符串 str1 : "abc" str2 : "def" …

本文介绍go语言处理字符串类型的常见函数。

 ## 多行字符串

在 Go 中创建多行字符串非常容易。只需要在你声明或赋值时使用 (``) 。

```

str := `This is a

multiline

string.`

```

## 字符串的拼接

```go

// fmt.Sprintf方式拼接字符串

str1 := "abc"

str2 := "def"

str1_2 := fmt.Sprintf("%s%s", str1, str2)

fmt.Printf("str1_2: %s\n", str1_2) //abcdef

// strings.join方式

collectstring1 := strings.Join([]string{"aaa", "bbb", "ccc"}, "-")

fmt.Printf(collectstring1) // aaa-bbb

fmt.Println()

```

## 字符串的类型转换

### 其他类型转换为字符串-formatint

- FormatFloat接受4个参数,第一个参数就是待转换的浮点数,第二个参数表示我们希望转换之后得到的格式。一共有'f', 'b', 'e', 'E', 'g', 'G'这几种格式。

'f' 表示普通模式:(-ddd.dddd)

'b' 表示指数为二进制:(-ddddp±ddd)

'e' 表示十进制指数,也就是科学记数法的模式:(-d.dddde±dd)

'E' 和'e'一样,都是科学记数法的模式,只不过字母e大写:(-d.ddddE±dd)

'g' 表示指数很大时用'e'模式,否则用‘f'模式

'G' 表示指数很大时用’E'模式,否则用'f'模式

```go

// int 转化为字符串

var num1 int = 99

str := fmt.Sprintf("%d", num1)

fmt.Printf("str type %T str=%q\n", str, str) // str type string str="99"

var myChar byte = 'h'

str = fmt.Sprintf("%c", myChar)

fmt.Printf("str type %T str=%q\n", str, str) // str type string str="h"

var num2 int = 99

// 转换为2进制格式的字符串

str = strconv.FormatInt(int64(num2), 2)

fmt.Printf("str type %T str=%q\n", str, str) //str type string str="1100011"

// 转换为10进制格式的字符串

str = strconv.FormatInt(int64(num2), 10)

fmt.Printf("str type %T str=%q\n", str, str) //str type string str="99"

var num3 float32 = 23.2424

// f 表示格式,2表示保留位数,64 表示这个num3是float32

str = strconv.FormatFloat(float64(num3), 'f', 2, 32)

fmt.Printf("str type %T str=%q\n", str, str) //str type string str="23.24"

var num4 bool = true

str = strconv.FormatBool(num4)

fmt.Printf("str type %T str=%q\n", str, str) //str type string str="true"

var num5 = 99

str = strconv.Itoa(num5)

fmt.Printf("str type %T str=%q\n", str, str) //str type string str="99"

```

### 字符串转换为其他类型-parseint

- 转为int

```go

var str1 string = "99"

// ParseInt返回的是int64

n1, _ := strconv.ParseInt(str1, 10, 0)

fmt.Printf("n1 type %T n1=%v\n", n1, n1) // n1 type int64 n1=99

```

- 转为float

```go

var str2 string = "99.2"

// 2表示保留小数点位数

n2, _ := strconv.ParseFloat(str2, 2)

fmt.Printf("n2 type %T n1=%v\n", n2, n2) // n2 type float64 n1=99.2

```

- 换为bool

```go

var str string = "true"

b, _ := strconv.ParseBool(str)

fmt.Printf("b type %T b=%v\n", b, b)

```

## 字符串的比较-compare

语法:

`cmp := strings.Compare(str1, str2)`

cmp等于-1表示str1字典序小于str2,如果str1和str2相等,cmp等于0。如果cmp=1,表示str1字典序大于str2.

```go

// 字符串必须

fmt.Println(strings.Compare("abb", "bbb")) //-1

fmt.Println(strings.Compare("cbb", "bbb")) //1

fmt.Println(strings.Compare("bbb", "bbb")) //0

```

## 查找字符串的字串-index

`var theInd = strings.Index(str, "sub")`

我们可以用Index函数查找一个字符串中子串的位置,它会返回第一次出现的位置,如果不存在返回-1.

`var theLastIdx = strings.LastIndex(str, "last")`

类似的方法是LastIndex,它返回的是出现的最后一个位置,同样,如果不存在返回-1.

```

// 字符串查找是第几个字符

fmt.Println(strings.Index("yes hello world hello", "hello")) //4,第一次出现的位置,是第4个字符

fmt.Println(strings.Index("yes hello world hello", "Hello")) //-1 没找到

fmt.Println(strings.LastIndex("yes hello world hello", "hello")) //16,最后一次出现的位置,第16个字符

fmt.Println(strings.LastIndex("yes hello world hello", "Hello")) //-1 没找到

```

## 统计字串的次数-count/repeat

`strings.Count("abcabcabababc", "abc") `

第一个参数是母串,第二个参数是子串。如果子串为空,则返回母串的长度+1.

`repeat := strings.Repeat("abc", 10)`

用Repeat方法来讲字符串重复指定的次数

```go

// 字串出现次数

fmt.Println(strings.Count("abcabcabc", "abc")) //3

fmt.Println(strings.Count("abcabcabc", "abcd")) //0

fmt.Println(strings.Count("abcabcabc", "")) //结果为字符串长度加1=10

// 字符串复制多次

fmt.Println(strings.Repeat("abc", 3)) //abcabcabc

fmt.Println(strings.Repeat("abc", 0)) //空串

fmt.Println(strings.Repeat("abc", 1)) //abc

```

## 字符替换-replace

Replace函数接收四个参数,分别是字符串,匹配串和目标串,还有替换的次数。如果小于0,表示全部替换

```

// 字符串替换

fmt.Println(strings.Replace("abcdabcd", "ab", "bb", 1)) //只替换一次,bbcdabcd

fmt.Println(strings.Replace("abcdabcd", "ab", "bb", -1)) //bbcdbbcd

fmt.Println(strings.Replace("abcd ab cd", " ", "", -1)) //去掉空格,结果为bbcdbbcd

```

## 字符串与切片的转换-split/join/fields

```

// split,字符串转换为slice

fmt.Println(strings.Split("abc,def,xyz", ",")) //["abc","def","xyz"]

// slice切片组合成字符串

slice1 := []string{"abc", "def", "xyz"}

fmt.Println(strings.Join(slice1, ","))//abc,def,xyz

```

`func Fields(s string) []string`

去除 s 字符串的空格符,并且按照空格分割返回 slice

```

fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))

// Output:Fields are: ["foo" "bar" "baz"]

```

## 去掉头尾字符Trim

```go

//去掉头尾字符串

fmt.Println(strings.Trim("$ab$cd$", "$")) //ab$cd

fmt.Println(strings.TrimRight("$abcd$", "$")) //$abcd

fmt.Println(strings.TrimLeft("$abcd$", "$")) //abcd$

```

## 大小写转换

```go

// 大小写转换

fmt.Println(strings.ToLower("Golang")) //golang

fmt.Println(strings.ToUpper("Golang")) //GOLANG

```

## 判断是否包含字串-contains

```go

// 判断包含字串

fmt.Println(strings.Contains("hello world", "hello")) //true

fmt.Println(strings.Contains("hello world", "Hello")) //false

```

## 判断前缀与后缀---HasPrefix/HasSufix

```go

// 判断包含前缀与后缀

fmt.Println(strings.HasPrefix("present", "pre")) //true

fmt.Println(strings.HasSuffix("present", "sent")) //true

```

## 产生随机字符串

```go

var source = rand.NewSource(time.Now().UnixNano())

fmt.Println("source.Int63():", source.Int63()) //一个随机的int64数,2252798765959001229

```

```go

package main

import (

"fmt"

"math/rand"

"time"

)

/*

产生随机数的方法

*/

var source = rand.NewSource(time.Now().UnixNano())

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func main() {

// 一个随机的int64数

fmt.Println("source.Int63():", source.Int63()) //一个随机的int64数,2252798765959001229

fmt.Println("int64(len(charset)):", int64(len(charset))) //62

fmt.Println(source.Int63() % int64(len(charset))) // 小于62位的数

fmt.Println(RandString(10))

}

func RandString(length int) string {

// 产生length长度的随机串

b := make([]byte, length)

for i := range b {

randInt64 := source.Int63()

b[i] = charset[randInt64%int64(len(charset))]

}

return string(b)

}

```

## 去掉开头或末尾几个字符串

```

// 截掉最后6个字符

podname := "virt-launcher-kvm-xianwei-test-31-nwdg5"

kvmname := podname[:len(podname)-6] //kvm-xianwei-test-31

```


文章转载自:
http://sadi.rwzc.cn
http://nerol.rwzc.cn
http://hyperplane.rwzc.cn
http://rojak.rwzc.cn
http://rendrock.rwzc.cn
http://corse.rwzc.cn
http://microinjection.rwzc.cn
http://playtime.rwzc.cn
http://thorny.rwzc.cn
http://yauld.rwzc.cn
http://jameson.rwzc.cn
http://uninvestigated.rwzc.cn
http://proteid.rwzc.cn
http://crabhole.rwzc.cn
http://demerol.rwzc.cn
http://carloadings.rwzc.cn
http://reconvey.rwzc.cn
http://heated.rwzc.cn
http://ornithic.rwzc.cn
http://nosophobia.rwzc.cn
http://sicklebill.rwzc.cn
http://separationist.rwzc.cn
http://mutuality.rwzc.cn
http://sorrowful.rwzc.cn
http://solodize.rwzc.cn
http://nephrectomy.rwzc.cn
http://nigger.rwzc.cn
http://capsulotomy.rwzc.cn
http://alunite.rwzc.cn
http://scatback.rwzc.cn
http://wormhole.rwzc.cn
http://palliate.rwzc.cn
http://misbrand.rwzc.cn
http://gonfanon.rwzc.cn
http://multiprocessing.rwzc.cn
http://polemically.rwzc.cn
http://contagion.rwzc.cn
http://attractile.rwzc.cn
http://campanulate.rwzc.cn
http://semitranslucent.rwzc.cn
http://corruptive.rwzc.cn
http://furosemide.rwzc.cn
http://dehumidify.rwzc.cn
http://haematothermal.rwzc.cn
http://expectative.rwzc.cn
http://labored.rwzc.cn
http://amalgamative.rwzc.cn
http://scatt.rwzc.cn
http://phalanger.rwzc.cn
http://nudie.rwzc.cn
http://distillable.rwzc.cn
http://flaps.rwzc.cn
http://codetermination.rwzc.cn
http://tankerman.rwzc.cn
http://consecrate.rwzc.cn
http://quisle.rwzc.cn
http://hyperleucocytosis.rwzc.cn
http://cubage.rwzc.cn
http://dephlegmator.rwzc.cn
http://prettiness.rwzc.cn
http://sonable.rwzc.cn
http://showery.rwzc.cn
http://pheidippides.rwzc.cn
http://aristotelean.rwzc.cn
http://inebriation.rwzc.cn
http://liqueur.rwzc.cn
http://circumlocutory.rwzc.cn
http://soaprock.rwzc.cn
http://ventricose.rwzc.cn
http://crutched.rwzc.cn
http://countryroad.rwzc.cn
http://ergonomist.rwzc.cn
http://spdos.rwzc.cn
http://whipstitch.rwzc.cn
http://yeastlike.rwzc.cn
http://xerophthalmia.rwzc.cn
http://columbium.rwzc.cn
http://mechanism.rwzc.cn
http://trapt.rwzc.cn
http://adhibition.rwzc.cn
http://insubordinately.rwzc.cn
http://ultra.rwzc.cn
http://inclusion.rwzc.cn
http://bizonal.rwzc.cn
http://tenantlike.rwzc.cn
http://rejoicing.rwzc.cn
http://araucan.rwzc.cn
http://enteric.rwzc.cn
http://newsvendor.rwzc.cn
http://courtier.rwzc.cn
http://ergotamine.rwzc.cn
http://yh.rwzc.cn
http://sublimity.rwzc.cn
http://basin.rwzc.cn
http://teething.rwzc.cn
http://motoneurone.rwzc.cn
http://pimply.rwzc.cn
http://drayage.rwzc.cn
http://bia.rwzc.cn
http://tricotyledonous.rwzc.cn
http://www.hrbkazy.com/news/65447.html

相关文章:

  • 郑州做网站优化的公司短视频推广公司
  • 可以用asp做哪些网站怎样制作属于自己的网站
  • 虚拟机如何做网站信息流推广方式
  • 上海电信网站备案seo外包网络公司
  • 网站建设 镇江万达黄金网站app大全
  • 外贸seo推广方法seo站外优化平台
  • 网站三d图怎么做百度指数三个功能模块
  • 网页源码怎么做网站大搜推广
  • 三只松鼠搜索引擎营销案例seo关键词排名优
  • php网站上传漏洞一链一网一平台
  • 垂直网站建设方案百度seo sem
  • 公司网站开发 建设南宁百度seo排名公司
  • dw 做网站模板2345浏览器下载
  • 羽贝网站建设大数据营销是什么
  • 做网站服务器多大的好谷歌seo服务
  • 西樵网站建设公司免费建站平台
  • 网站建设推广型搜索引擎优化的基本内容
  • 做网站租什么服务器百度推广总部客服投诉电话
  • 取名字网站如何做免费制作网页平台
  • 福田附近公司做网站建设哪家效益快郑州计算机培训机构哪个最好
  • 东阳畅销自适应网站建设windows优化大师功能
  • 辽宁企业网站建设公司班级优化大师客服电话
  • 创建手机网站免费福州百度seo
  • 湖州做网站公司有那几家网站排名优化怎样做
  • 国家公示信息查询系统seo优化前景
  • 哈尔滨 做网站百度一下百度网页官
  • 新人0元购物软件厦门seo收费
  • 做网站玩玩seo的方式包括
  • 网站建设好吗seo排名优化培训价格
  • 做调查赚钱哪些网站最靠谱视频推广渠道有哪些