//: 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. cf C On Number of Decompositions into Multipliers

    题意:给你n个数,然后把这个n个数的乘积化成n个数相乘,可以化成多少个. 思路:分解质因数,求出每一个质因子的个数,然后用组合数学中隔板法把这些质因子分成n分,答案就是所有质因子划分成n份的情况的乘积 ...

  2. Beauty Contest(graham求凸包算法)

    Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25256   Accepted: 7756 Description Bess ...

  3. 【转】android颜色对应的xml配置值

    原文网址:http://www.cnblogs.com/etgyd/archive/2011/04/02/2003778.html android颜色对应的xml配置值 <?xml versio ...

  4. 分治(CDQ):[BOI2007]摩基亚Mokia

    [题目描述] 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它能 ...

  5. Sort List ——LeetCode

    Sort a linked list in O(n log n) time using constant space complexity. 链表排序,要求时间复杂度O(nlgn),我写的归并排序. ...

  6. Find Minimum in Rotated Sorted Array——LeetCode

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  7. CodeForces 592B

    题目链接: http://codeforces.com/problemset/problem/592/B 这个题目没啥说的,画图找规律吧,哈哈哈 程序代码: #include <cstdio&g ...

  8. 转:VS2010解决方案转换到VS2008

    原文链接地址:http://www.codeproject.com/Tips/80953/Converting-VS2010-Solution-to-VS2008 如果你使用VS2010的任何版本写代 ...

  9. 路由器刷机常见第三方固件及管理前端种类(OpenWrt、Tomato、DD-Wrt)

    目前路由器折腾刷机,除了采用各品牌的原厂固件外,第三方路由器固件,基本就是:Tomato.DD-WRT.OpenWRT三种. 基本上所有第三方路由器固件的架构上可分为前端(Frontend)和后端(B ...

  10. viewWillLayoutSubView

    当viewController的bounds又改变,调用这个方法来实现subview的位置.可重写这个方法来实现父视图变化subview跟着变化.                   > Lif ...