go语言range字符串 go语言字符串转换成数字

如何理解Golang中的range语句

你把它理解为遍历么,结合for循环。

创新互联主营武威网站建设的网络公司,主营网站建设方案,成都app开发,武威h5重庆小程序开发搭建,武威网站营销推广欢迎武威等地区企业咨询

假设有一个初始化好的数组(table)或者切片(slice)的table,且table长度为10:

for i, value := range table {

fmt.Printf("i=%v, value=%v\n", i, value)

}

则会执行fmt.Printf10次,且这10次的【i】的值分别是从0~9,也就相当于

for i := 0; i  len(table); i++ {

fmt.Printf("i=%v, value=%v\n", i, table[i])

}

如果把上边的数组或者切片换成map

for key, value := range table {

fmt.Pritnf("key=%v, value=%v\n", key, value)

}

则类似上边的过程把map里边的key-value键值对一 一遍历

如何使用Range截取字符串

func someMethodOfRange() {

/**

* 字符串与Range的用法

*/

//1.截取字符串(字符)在整个字符串中所在的位置,返回的类型为 Index

var str = "abcdefghi"

let startIndex = str.rangeOfString("cde")!.startIndex

print(startIndex)//2

let endIndex = str.rangeOfString("cde")!.endIndex

print(endIndex)//5

//2、根据字符串的索引来获取对应的字符

let text = "abcdefghi"

let index2 = text.startIndex.advancedBy(2) //will call succ 2 times

print(index2)//2

let lastChar: Character = text[index2] //now we can index!

print(lastChar)//c

let lastChar2 = text.characters[index2] //will do the same

print(lastChar2)//c

//3.截取字符串(字符)在整个字符串中所在的范围 、返回值类型RangeIndex

let range: RangeString.Index = text.rangeOfString("bc")!

print(range)//1..3

let index3: Int = text.startIndex.distanceTo(range.startIndex)

print(index3)//1

let index4: Int = text.startIndex.distanceTo(range.endIndex)

print(index4)//3

print(text.characters.count)//9

let abc = "@救123"

print(abc.characters.count)//5

//4.0 Index -- Int

let range: RangeString.Index = self.text!.rangeOfString(text)!

let index3: Int = self.text!.startIndex.distanceTo(range.startIndex)

let index4: Int = self.text!.startIndex.distanceTo(range.endIndex)

//一个字符串。指标值可以通过调用predecessor()方法访问其 先前 指标,其指标successor()下一个指标 调用方法。字符串中的任何指标可从任何其他指数通过把这些方法结合在一起,或通过使用--(_:)方法。试图访问一个字符串的范围以外的索引会引发一个运行时错误

let greeting = "123456789"

greeting[greeting.startIndex]

print(greeting)

print("起使位置\(greeting.startIndex)")//0

print("终点位置\(greeting.endIndex)")//9

print(greeting[greeting.startIndex])//1

greeting[greeting.endIndex.predecessor()]

print(greeting[greeting.endIndex.predecessor()])//9

print(greeting.endIndex.predecessor())//8

print(greeting.startIndex.successor())//1

greeting[greeting.startIndex.successor()]

print(greeting[greeting.startIndex.successor()])//2

let index = greeting.startIndex.advancedBy(7)

greeting[index]

print(greeting[index])//8

//试图在字符串的范围以外的索引处访问一个字符,将引发一个运行时错误。”

//greeting[greeting.startIndex.predecessor()]//cuowu

//print(greeting[greeting.endIndex])//cuowu

//越界

//greeting[greeting.endIndex]

//greeting.endIndex.successor()

//““Use the indices property of the characters property to 创建一个字符串中访问单个字符的所有索引的范围。”

for index in greeting.characters.indices {

print("\(greeting[index])",terminator: "")

// print("**")

}

}

//相关的应用:在UILabel里面,不同文本显示不同字体的大小

extension UILabel {

/*

*设置text的字体大小

*/

func settingLabelTextKitWithText(text:String,font:UIFont) {

if text.isEmpty {//通过检查其 Boolean 类型的 isEmpty 属性来判断该字符串是否为空:

print("什么都没有")

return

}else {

//方法1

let attrstring:NSMutableAttributedString =NSMutableAttributedString(string: self.text!)

let str = NSString(string: self.text!)

let theRange = str.rangeOfString(text)

attrstring.addAttribute(NSFontAttributeName, value: font, range: theRange)

self.attributedText = attrstring

/**

// 方法2

let attrstring:NSMutableAttributedString = NSMutableAttributedString(string: self.text!)

let range: RangeString.Index = self.text!.rangeOfString(text)!

let index3: Int = self.text!.startIndex.distanceTo(range.startIndex)

let index4: Int = self.text!.startIndex.distanceTo(range.endIndex)

attrstring.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(index3, index4 - index3))

self.attributedText = attrstring

*/

}

}

go语言怎么输出字符串中的某个中文字符?

for index,val := range a {

if val == '好' {

fmt.println(index,x)

}

}

对string做range得到的val是int32类型,直接用单引号比较就行


文章标题:go语言range字符串 go语言字符串转换成数字
文章出自:http://scyanting.com/article/ddjcsoc.html