Swift introductory article-strings and characters

Source: Internet
Author: User

Today is mainly about the use of strings, the syntax of the string and object-c syntax is not the same, but the idea is the same, that is, the wording is not the same. If you are familiar with. NET and Java syntax, then you have little deep pressure. If you are not clear about the basic type of swift, please consult.

Introduction to Swift-Basic type (1) Swift Introductory article-basic type (2) Swift Introductory article-basic type (3)

One: Create an empty string

-------------define an empty string the first method var str1= ""//-------------define an empty string the second method var  str2 = string ()

Run results

Str1=str2=

Two: Determine if the string is empty (IsEmpty)

-----------determine if the string is empty var str3:string = "" If Str3.isempty {//isempty is a property of the string, determines whether the string is empty    println ("The value of Str3 is empty")} else{    println ("The value of STR3 is NOT NULL")}

Run results

The value of the STR3 is empty

Three: Escape character of a string

"\ \ \ \" \ \ \ backslash   \ \ t tab \ n newline character \   r    return character \ "  double quotation mark \"  single quotation mark

Example

------------------line break----------------var  stra = "Hello world \ n  1"/\ n is the newline character println ("stra=\ (stra)")//---- --------------double quotes----------------var  strB = "\" Double quotation mark "//\" is a double quote println ("strb=\ (StrB)")//------------------ Single quote----------------var  strc = "\ ' single quote"//\ ' stands for single quote println ("strc=\ (STRC)")

Run results

Stra=hello World   1strb= "double quote strc= ' single quote

Three: string concatenation

1: Multiple string variable splicing with + to splice

2: String variables and Constants are stitched together to form a new string with + or + =

Example:

-----------multiple string variables splicing with + to splice var  str4 = "Hello" var  str5 = "Swift" var  str6 = STR4+STR5//String variable stitching println (" Str6=\ (STR6)//-----------string variables and constants with + or + = to splice var  str7= "Hello" str7 + = "Swift"//string variable and character constant splicing can be used with + = splicing println (" Str7=\ (STR7) ")

Run results

Str6=hello Swiftstr7=hello Swift

4: string and placeholder variables make up a new string

var a = 3//defines an integer variable var strq = "A = \ (a)"//string and an external variable, forming a new variable (representing the external variable (variable)) println ("\ (STRQ)")

Run results

A = 3

5: string comparison

1: string equality = = 2: Prefix equal Hasprefix () 3: Suffix equal hassuffix ()

Example

Import Foundationvar  stra = "Hello" var  strB = "Hello"//-----------string equal = =-------if  stra = = strb{    println ("string-equal")}else{    println ("string-unequal")}//-----------string prefixes equal hasprefix---------if Stra.hasprefix ("H") {    println ("String prefix-equality")}else{    println ("string prefix-unequal")}//-----------string suffix equal hassuffix---------if Stra.hassuffix ("O") {    println ("String suffix-equal")}else{    println ("string suffix-unequal")}

Run results

string-equal string prefix-equal string suffix-equal

Six: string case conversion

Uppercasestring Caps
LowerCaseString lowercase

Import Foundationvar  stra = "Hello"//-----------string capitalization conversion var  strB = stra.uppercasestring//uppercasestring String uppercase conversion println (StrB)//------------string lowercase conversion var strc = stra.lowercasestring//lowercasestring string conversion lowercase println (STRC)

Run results

Hellohello

Seven: Characters

Format: variable keyword and constant keyword  variable: Character =  character value Note: Character values must be enclosed in double quotation marks and must be a single character.

Example

var ch:character = "C"  //character value must be in double quotes, and is a character println ("ch=\ (CH)")

Run results

Ch=c

The relationship between strings and characters:

A string is made up of N characters, that is, the string is a collection of characters.

Example:

var str = "ABC"  //String "ABC" is composed of three characters, respectively, character "A", character "B", character "C"

String Traversal ( for in)

Description: For in  is a traversal statement  1:for followed by a temporary variable   in followed by array 2: The temporary variable does not need to be defined, and the compiler automatically generates a temporary variable

3:for in will iterate through the character set and assign each set a temporary variable

Example:

Import Foundationvar str = "ABC"/*1:str is a string variable is a character set  2:temp is a temporary variable 3:for in will iterate through the character set and then assign each set a temporary variable temp*/for temp  In str {    println (temp)}

Run results

Abc

Character and string concatenation (+)

Character and string concatenation, with + direct stitching, compose a new string (and string concatenation usage has been)

Example

Import Foundationvar c:character = "S"//Definition C is a character variable var str = c + "Hello" println (str)

Run results

S Hello

I have come back to learn from the Swift language of the knowledge to write to form a series. As a new language, personal understanding inevitably has shortcomings, welcome to give me advice. can also add my QQ 14,360,511,081 discussion, if you have any problem, you can also directly in QQ message sent to me, I saw the first time after you reply

Finally, a summary. Send the mind map as the end of the article

Swift introductory article-strings and characters

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.