Swift学习笔记 - 字符串

简介: 1. 不可变字符串 Objective-C: NSString *string1 = @"Hello World!"; Swift: let string1 = "Hello world!"   2.

 

1. 不可变字符串

Objective-C:

NSString *string1 = @"Hello World!";

Swift:

let string1 = "Hello world!"

 

2. 可变字符串

Objective-C:

NSMutableString *string2 = [NSMutableString new];
NSMutableString *string3 = [@"" mutableCopy];

Swift:

var string2 = String()
var string3 = ""

 

3. 输出

Objective-C:

NSLog(@"%lu", (unsigned long)string1.length); // Prints "12"
NSLog(@"%i", !string2.length);                 // Prints "1"
NSLog(@"%i", [string1 hasPrefix:@"Hello"]); // Prints "1"
NSLog(@"%i", [string1 hasSuffix:@"earth!"]);// Prints "0"

Swift:

println(string1.utf16Count)                // Prints "12"
println(countElements(string1))            // Prints "12"
println(string2.isEmpty)                // Prints "true"
println(string1.hasPrefix("Hello"))     // Prints true
println(string1.hasSuffix("earth!"))     // Prints false

 

4. 比较

Objective-C:

// Prints "string2 equals string3"
if ([string2 isEqualToString:string3]) {
    NSLog(@"string2 equals string3");
}

Swift:

// Prints "string2 equals string3"
if string2 == string3 {
    println("string2 equals string3")
}


5. 转换

Objective-C:

NSString *fiveString = [@5 stringValue]; // "5"
NSInteger five = [fiveString integerValue];

Swift:

let fiveString = "\(5)"                         // "5"
let five = fiveString.toInt()!
let pi = Double((π as NSString).doubleValue)     // 3.14

 

6. Copy and Mutable Copy

Objective-C:

NSMutableString *string4 = [string1 mutableCopy];
[string4 appendFormat:@"Am I alone?"];
NSLog(@"%@", string1);        // Prints "Hello world!"
NSLog(@"%@", string4);        // Prints "Hello world! Am I alone?"
NSMutableString *string5 = string4;
[string5 replaceCharactersInRange:NSMakeRange(13, 10)
    withString:@"How do you like me now"];
// Prints "Hello world! How do you like me now?"
if ([string4 isEqualToString:string5]) {
    NSLog(@"%@", string5);
}

NSMutableString *tempHigh = [@"85" mutableCopy];
NSMutableString *tempLow = [@"70" mutableCopy];
NSString *degreeF = @"\u2109";
[tempHigh appendString:degreeF];
[tempLow appendString:degreeF];

// Prints "High/Low: 85°F/70°F"
NSLog(@"High/Low: %@/%@", tempHigh, tempLow); 

Swift:

var string4 = string1
string4 += " Am I alone?"
println(string1)         // Prints "Hello world!"
println(string4)         // Prints "Hello world! Am I alone?"
var string5 = string4
let startIndex = advance(string5.startIndex, 13)
string5.replaceRange(startIndex..<string5.endIndex, with: "How do you like me now?")
// Prints "Hello world! How do you like me now?"
if string4 != string5 {
    println(string5)
}


var tempHigh = "85"
var tempLow = "70"
let degreeF: Character = "\u{2109}"
tempHigh.append(degreeF)
tempLow.append("\u{2109}" as Character)
// Prints "High/Low: 85°F/70°F"
println("High/Low: \(tempHigh)/\(tempLow)") 

 

目录
相关文章
|
编译器 Swift iOS开发
10 Swift中的字符串
Swift中的字符串
79 0
|
5天前
|
Swift iOS开发
Swift 字符串
10月更文挑战第26天
16 3
|
Java Swift iOS开发
Swift - 字符串
Swift - 字符串
108 0
|
Swift 索引
Swift实用小册03:字符串的使用
Swift实用小册03:字符串的使用
225 0
Swift实用小册03:字符串的使用
|
存储 Swift
Swift5.1—字符串的Unicode表示形式
Swift5.1—字符串的Unicode表示形式
451 0
Swift5.1—字符串的Unicode表示形式
|
存储 Swift
Swift5.1—子字符串
Swift5.1—子字符串
347 0
Swift5.1—子字符串
|
编译器 Swift
Swift5.1—字符串字面量
Swift5.1—字符串字面量
160 0
Swift5.1—字符串字面量
|
Swift
swift微博第2天(命名空间和控制器字符串)
swift微博第2天(命名空间和控制器字符串)
147 0
swift微博第2天(命名空间和控制器字符串)
|
Swift
Swift学习笔记——页面跳转
创建一个single view app后,项目中有main.storyboard,里面是一个viewcontroller。 那么我们如何实现页面跳转 首先添加一个导航控制器Navigation Controller。点击右上的➕,然后选择控件拖到面板上即可
1236 0
|
前端开发 测试技术 Swift
Swift学习笔记——新建项目
在xcode菜单中选择 new -> project -> single view app 点击next在弹出窗中填写项目名称 这里languge有可以选择object-c或swift作为项目语言。这里我们学习swift,所以选择swift。 如果language选择swift,下面的user interface可以选择swiftUI和storyboard。 SwiftUI是2019 年 WWDC 大会上,苹果在压轴环节向大众宣布了基于 Swift 语言构建的全新 UI 框架,与flutter类似,是用代码编写页面,支持快速预览。
725 0