//: Playground - noun: a place where people can play

import UIKit

var str = "Welcome to Play Swift! Step by Step learn Swift language from now!"

// range
str.rangeOfString("Step") // 默认从前面开始搜索
str.rangeOfString("Step", options: NSStringCompareOptions.BackwardsSearch, range: nil, locale: nil) // 从后面开始搜索 str.startIndex
str.endIndex
var strRange = Range<String.Index>(start: str.startIndex, end: str.endIndex) // 搜索特定范围中特定字符串的范围
var searchStartIndex:String.Index = str.startIndex // 搜索开始的位置
var searchEndIndex:String.Index = searchStartIndex.advancedBy(7) // 搜索结束的位置
let searchRange = Range<String.Index>(start: searchStartIndex, end: searchEndIndex)
str.rangeOfString("Step", options: NSStringCompareOptions.CaseInsensitiveSearch, range: searchRange, locale: nil) // subString
var toIndex = str.startIndex.advancedBy(4)
str.substringToIndex(toIndex) var fromIndex = str.startIndex.advancedBy(14)
str.substringFromIndex(fromIndex) str.substringWithRange(searchRange) // insert
var insertIndex = searchStartIndex.advancedBy(22)
str.insert("!", atIndex: insertIndex); // remove
str.removeAtIndex(insertIndex)
str.removeRange(searchRange) // replace
var replaceRange = Range<String.Index>(start: str.startIndex, end: str.startIndex.advancedBy(14))
str.stringByReplacingCharactersInRange(replaceRange, withString: "Rinpe Chan") // String.Index 也是一种数据类型

  

Swift - 16 - String.Index和Range的更多相关文章

  1. Swift 中 String 取下标及性能问题

    Swift 中 String 取下标及性能问题 取下标 String String 用 String.Index 取下标(subscript)得到 Character,String.Index 要从 ...

  2. java.lang.StringIndexOutOfBoundsException: String index out of range: 0

    hibernet 报错 java.lang.StringIndexOutOfBoundsException: String index out of range: 0 处理方法  数据表字段为char ...

  3. Spring Boot 启动报错 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 37

    使用命令 java -jar springBoot.jar  启动项目,结果报错如下: Exception at java.lang.String.substring(String.java:) at ...

  4. IndexError:string index out of range

    IndexError:string index out of range 出现在下标越界的情况,如 item[1],可能为空的时候下标就会越界

  5. hibernate createSQLQuery StringIndexOutOfBoundsException: String index out of range: 0

    有一个sql用union拼接的如下: select id,(**还有很多字段**),'' as NewName from tb1 union select id,(**还有很多字段**),name a ...

  6. mac安装MySQLdb:IndexError: string index out of range

    使用mac安装MySQLdb的时候出现string index out of range 大概的错误是这样的: 然后尝试手动安装,我下载了包后,依然出现这个错误. 于是百度了下: https://ww ...

  7. tk.mybatis 报错:tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiLogMapper ...

  8. [Swift]字符串(String类、NSString类)常用操作

    NS是Cocoa类对象类型的前缀,来源于乔布斯建立的另一家公司--NeXTNSString的使用方法,和Swift语言中的String有很多相似之处. 1.字符串的定义String类 var str1 ...

  9. Swift 01.String

    1.字符串拼接 var num1 = "hello,world" var name = "xiaoming" var age = let student = n ...

随机推荐

  1. 我的HttpClients工具

    import java.io.IOException; import javax.ws.rs.core.MediaType; import org.apache.commons.httpclient. ...

  2. javascript类型系统之Array

    原文:javascript类型系统之Array 目录 [1]数组创建 [2]数组操作 [3]继承的方法 [4]实例方法 数组转换 数组检测 栈和队列 排序方法 操作方法 位置方法 前面的话 数组是一组 ...

  3. Android Wear开发 - 数据通讯 - 第一节 : 连接数据层

    http://developer.android.com/training/wearables/data-layer/accessing.html Accessing the Wearable Dat ...

  4. 【调侃】IOC前世今生(转)

    前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...

  5. 【Linux】鸟哥的Linux私房菜基础学习篇整理(十)

    1. at [-mldv] TIME/at -c 工作号码:单一工作调度.参数:-m:当at的工作完成后,即使没有输出信息,以email通知用户该工作已完成:-l:相当于atq,列出目前系统上面的所有 ...

  6. ♫【Backbone】this

    Backbone.js Event Binding MyView = Backbone.View.extend({ events: { 'click .item': 'handleClick' }, ...

  7. 数学(矩阵乘法,随机化算法):POJ 3318 Matrix Multiplication

    Matrix Multiplication Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17783   Accepted: ...

  8. form.Show()和form.ShowDialog()的区别、新建一个form和MessageBox.Show()的常见用法

    一:form.Show()和form.ShowDialog()的区别 a. 任何窗体(派生于基类Form的类),都可以以两种方式进行显示. //非模式窗体From qform=new Form();q ...

  9. HDOJ(HDU) 2106 decimal system(进制相互转换问题)

    Problem Description As we know , we always use the decimal system in our common life, even using the ...

  10. 差别不在英语水平,而在汉语水平If you do not leave me, we will die together.

    为什么高考语文要提高到180分,英语降到100,差别不在英语水平,而在汉语水平.看下面例句的译法: If you do not leave me, we will die together. 你如果不 ...