swift字符串的截取以及Range的用法

更新时间:2023-07-18 03:21:04 阅读: 评论:0

swift字符串的截取以及Range的⽤法
func someMethodOfRange() {
/**
*  字符串与Range的⽤法
*/
//1.截取字符串(字符)在整个字符串中所在的位置,返回的类型为 Index
var str = "abcdefghi"
let startIndex =  str.rangeOfString("cde")!.startIndex
print(startIndex)//2
let endIndex =  str.rangeOfString("cde")!.endIndex
budgetingprint(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
piss
let lastChar2 = text.characters[index2] //will do the same
print(lastChar2)//c
//3.截取字符串(字符)在整个字符串中所在的范围、返回值类型 Range<Index>
let range: Range<String.Index> = text.rangeOfString("bc")!
print(range)//1..<3
let index3: Int = text.startIndex.distanceTo(range.startIndex)
print(index3)//1
let index4: Int = text.startIndex.dIndex)
print(index4)//3
print(unt)//9
let abc = "@救123"
print(unt)//5
//4.0 Index --> Int
procedure什么意思let range: Range<String.Index> = lf.text!.rangeOfString(text)!
let index3: Int = lf.text!.startIndex.distanceTo(range.startIndex)
let index4: Int = lf.text!.startIndex.dIndex)
/
/⼀个字符串。指标值可以通过调⽤predecessor()⽅法访问其先前指标,其指标successor()下⼀个指标调⽤⽅法。字符串中的任何指标可从任何其他指数通过把这些⽅法结合在⼀起,或通过使⽤--(_:)⽅法。试图访问⼀个字符串的范围以外的索引会引发⼀个运⾏时错误let greeting = "123456789"
greeting[greeting.startIndex]
print(greeting)
print("起使位置\(greeting.startIndex)")//0
print("终点位置\(dIndex)")//9
print(greeting[greeting.startIndex])//1
dIndex.predecessor()]
print(dIndex.predecessor()])//9
dIndex.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(dIndex])//cuowu
//越界
//dIndex]
/
/dIndex.successor()
//““U the <indices> property of the <characters> property to 创建⼀个字符串中访问单个字符的所有索引的范围。”
for index in greeting.characters.indices {
print("\(greeting[index])",terminator: "")
//            print("**")
}
}
//相关的应⽤:在UILabel⾥⾯,不同⽂本显⽰不同字体的⼤⼩
extension UILabel {
/*
*设置text的字体⼤⼩
*/
func ttingLabelTextKitWithText(text:String,font:UIFont) {
if text.isEmpty {//通过检查其 Boolean 类型的 isEmpty 属性来判断该字符串是否为空:
print("什么都没有")
return
}el {
//⽅法1
let attrstring:NSMutableAttributedString = NSMutableAttributedString(string: lf.text!)
let str = NSString(string: lf.text!)
let theRange = str.rangeOfString(text)
attrstring.addAttribute(NSFontAttributeName, value: font, range: theRange)
小学四年级英语下册
lf.attributedText = attrstringhomework什么意思
/**
//  ⽅法2
let attrstring:NSMutableAttributedString = NSMutableAttributedString(string: lf.text!)
let range: Range<String.Index> = lf.text!.rangeOfString(text)!
let index3: Int = lf.text!.startIndex.distanceTo(range.startIndex)
restaurant是什么意思
let index4: Int = lf.text!.startIndex.dIndex)
attrstring.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(index3, index4 - index3))                lf.attributedText = attrstring
*/
}discuss的用法
bole}
cpsp

本文发布于:2023-07-18 03:21:04,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/180784.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   字符   访问   范围   位置   类型
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图