Code Monkey home page Code Monkey logo

logcatty's Introduction

字符串创建

字符串支持动态修改,拼接:

/// String 初始化
var strVal = String()
if strVal.isEmpty
{
    print("this is empty")
}
strVal += "Hello World!"

对于String和Character,支持String遍历:

// 使用for-in遍历所有字符
for c in strVal
{
    print(c)
}
// 获取字符串长度
print("the characters count:\(strVal.count)")

访问字符串

字符串中各位字符支持下标访问,常用的有:

  • stringData[indexer]
  • stringData.startIndex
  • stringData.endIndex
  • stringData.index(before:)
  • stringData.index(after:)
  • stringData.index(indexer, offsetBy:) 示例:
// 字符串索引
var strData : String = "Test_string_data"
var a = strData[strData.index(after: strData.startIndex)]       // e
var b = strData[strData.index(before: strData.endIndex)]        // a
var c = strData[strData.index(strData.startIndex, offsetBy: 5)] // s

修改字符串

插入

调用insert(_:at:)可以在一个字符串的指定索引位置插入一个字符,而调用insert(contentOf:at:)可以在一个字符串的指定索引位置插入一段字符串。

var welcomeStr = "Helloworld"
// 插入字符
welcomeStr.insert(",", at: welcomeStr.index(welcomeStr.startIndex, offsetBy: 5))
print(welcomeStr)
//插入字符串
welcomeStr.insert(contentsOf: "Jian", at: welcomeStr.endIndex)
print(welcomeStr)

删除

调用remove(at:)可以在一个字符串的指定索引位置删除一个字符,而调用removeSubrange(_:)可以在一个字符互传的指定位置删除一个子字符串。

var welcomeStr = "Hello,world"
// 删除字符
welcomeStr.remove(at: welcomeStr.index(welcomeStr.startIndex, offsetBy:5))
print(welcomeStr)
// 删除字符串
welcomeStr.removeSubrange(welcomeStr.index(welcomeStr.startIndex, offsetBy: 5)..<welcomeStr.endIndex)
print(welcomeStr)

字符串及引用

使用下标或者prefix(_:)之类的方法可以得到一个Substring实例,并非一个String,在临时变量中可以使用Substring,如果需要持久持有则需要转化为String。 换句话说,String在内存中有一块独立的空间,Substring则仅仅是一个指向该内存的引用,本身并不会真实创建并持有一块专有的内存空间。
stringSubstring_2x.png

字符串比较

swift支持三种方式比较文本:

  • 字符串相等,使用== 或者 !=
  • 字符串前缀,使用 hasPrefix(_:)
  • 字符串后缀,使用 hasSuffix(_:)
var welcomeStr = "Hello,world"
var welcomeStr2 = "Hello,world2"
print(welcomeStr == welcomeStr2)
print(welcomeStr.hasPrefix("Hello2"))
print(welcomeStr.hasSuffix("world"))

logcatty's People

Contributors

jiache02 avatar riskycheng avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.