Swift Learning Notes (14)--characters, constant strings and variable strings

Source: Internet
Author: User

In the course of learning Java, strings have encountered string and StringBuffer, the former is immutable, the string cannot be modified, the latter is mutable, and can be modified continuously. In Swift, the definition of a string becomes simpler.

(1) Overview

In Swift, the string constants are declared with Let and cannot be modified. A string variable is declared with Var and can be modified. demonstrated through code.

Let str1 = "Hello1" var str2 = "Hello2" str1 = "world1"//Error: Cannot assign to ' let ' value ' str1 ' str2 = ' world2 ' str1 + = "World 1 "//error: binary operator ' + = ' cannot is applied to the String operands str1 + = ' world1 ' str2 + =" World2 "

The string that the let declares is immutable and the string variable of the Var declaration is visible by the code.


(2) String initialization

There are two types of initialization methods for swift strings, the first is to assign null values, and the second is to instantiate with the string () class. The code is as follows:

var str3= "" var str4 = String ()

(3) The string is null-judged

Sometimes the code needs to determine if a string is empty, and you can use the IsEmpty () method. The code and output are as follows:

var str1 = "Hello" let str2 = "Hello" var str3 = "" var STR4 = String () str1.isempty  //Output falsestr2.isempty  //Output falsest R3.isempty  //Output truestr4.isempty  //Output True


(4) looping through strings using for-in loops

var str1 = "Hello"//Use for-in loop to traverse string for index in str1{    println (Index)}

The output results are as follows:


(5) Swift character

As with the C language, there is also the concept of character in Swift, using the keyword character declaration. The code is as follows:

var ch:character = "h" println (CH)

The resulting output is as follows:


A string can also be added using the Append () method, but you cannot add a string using the Append () method. The code is as follows:

var ch:character = "H" var str1:string = "Hello" var str2:string = "World" str1.append (CH)//via Str1.append (STR2)//error: Cannot Invoke ' append ' with a argument list of type ' (String) '
The output results are as follows:


(6) Use of the + = operator in a string

There is an important operator in the string: + =, which can be used to stitch two strings. However, you cannot splice characters.

The code is as follows:

var ch:character = "H" var str1:string = "Hello" var str2:string = "World" str1 + = str2//through, can connect two strings str1 + = ch//error such as:


After the last string concatenation, the output results:


(7) Calculating string length

The Xcode version I'm using is the most recent version 6.4 (6e35b), and this method may differ in different versions of Xcode and Swift. Online says you can use Countelements (str) to calculate the string length, but the pro-test is not applicable in my version, Xcode6.4 can only use the Count (str) method to calculate the string length. and count () cannot calculate the length of the character character because the character defaults to 1 and does not need to be evaluated. You can try the countelements (), count () Two methods in your environment.

The code is as follows:

var str1:string = "Hello" var str2:string = "Hello" count (str1) count (str2)

The output results are as follows:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift Learning Notes (14)--characters, constant strings and variable strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.