原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-ID285

1、字符串字面值

 let someString = "Some string literal value"

2、初始化空的字符串

两种方法:

 var emptyString = ""               // empty string literal
var anotherEmptyString = String() // initializer syntax
// these two strings are both empty, and are equivalent to each other

可以通过isEmpty属性检查字符串是否为空:

 if emptyString.isEmpty {
print("Nothing to see here")
}
// prints "Nothing to see here"

3、字符串的可变性

通过声明为常量或变量来判断字符串是否可变。

 var variableString = "Horse"
variableString += " and carriage"
// variableString is now "Horse and carriage" let constantString = "Highlander"
constantString += " and another Highlander"
// this reports a compile-time error - a constant string cannot be modified

这种方式与Objective-C不同。Objective-C需要用NSString和NSMutableString两种类来分别表示不可变字符串和可变字符串。

4、字符串是值类型

Swift 的 String 类型是值类型。如果你创建了一个新的字符串值,那么当其进行常量、变量赋值操作或在函数中传递时,会进行值拷贝。在不同情况下,都会对已有字符串值创建新副本,并对该新副本进行传递或赋值。

Swift 默认字符串拷贝的方式保证了在函数/方法中传递的是字符串的值,这种方式保证了你可以独有该字符串的值,无论它来自哪里,传递给你的字符串本身不会被更改,除非是你自己更改它。
在实际编译时,Swift编译器会优化字符串的使用,使实际的复制只发生在绝对必要的情况下,这意味着你始终可以在将字符串作为值类型的同时获得极高的性能。

5、使用字符

通过遍历字符串的characters属性,可以访问字符串的单个字符:
 for character in "Dog!												

Swift3.0P1 语法指南——字符串与字符的更多相关文章

  1. Swift3.0P1 语法指南——枚举

    原档: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programm ...

  2. Swift3.0P1 语法指南——闭包

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  3. Swift3.0P1 语法指南——构造器

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  4. Swift3.0P1 语法指南——属性

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  5. Swift3.0P1 语法指南——类和结构体

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  6. Swift3.0P1 语法指南——基本操作符

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  7. Swift3.0P1 语法指南——下标

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  8. Swift3.0P1 语法指南——方法

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  9. Swift3.0P1 语法指南——函数

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

随机推荐

  1. vi编辑文件出现Can't open file for writing错误

    可以用 ll 命令查看一下文件的权限,很有可能是没有权限,用chmod命令修改一下权限就可以了(当然是文件所有者或者root用户才能修改),或者切换成root用户(不推荐)

  2. 个人作业—Week3

    博客阅读体会 阅读了十几位软件工程师前辈的博文,了解了前辈们作为一名软件工程师的成长经历,我有一些感触. 这十几位前辈们的经历有着很大的差别,有的科班出身,有的则完全自学成才.不同的经历使得前辈们看问 ...

  3. primefaces p:dataExporter filename 支持中文 utf8

    p:fileDownload and p:dataExporter : for p:fileDownload, the Content-Disposition header should be set ...

  4. JavaWeb---总结(十六)JSP指令

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  5. cobbler深入学习

    cobbler重要目录和cobbler各对象的关系 /var/www/cobbler/ks_mirror 存放操作系统镜像/var/www/cobbler/repo_mirror 存放仓库镜像/var ...

  6. linux程序处理po多语言的两种脚本配置方式

    1.在configure.ac里面配置ALL_LINGUAS,然后调用AM_GLIB_GNU_GETTEXT 2.在po目录下面放置LINGUAS文件,由gettextize来生成并处理

  7. Module模式

    <script> var myModel=(function(){ var model={}; var privateVar="Hello World"; functi ...

  8. 日志分析 第一章 ELK介绍

    1 ELK各组件介绍? ELK Stack是elasticsearch.logstash.kibana是三个开源软件的组合, fielbeat是一个轻量级日志收集工具,类似于Linux系统中tail ...

  9. centos nc命令安装

    yum install nc.x86_64 nc命令的参数 参数 作用-i 设置数据报传送时间间隔-l 以服务器方式运行-k 重复接收并处理某个端口上的所有连接,必须与-l选项一起使用-n 使用ip地 ...

  10. linux手动或者自动启动oracle11g的服务 Oracle 自动启动脚本

    手动启动: [oracle@localhost ~]$ sqlplus SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 26 23:39:52 2 ...