Powerful swift string and powerful swift string

Source: Internet
Author: User

Powerful swift string and powerful swift string

Swift is a collection of hundreds of experts, absorbing the good features of mainstream languages such as java, c, and c ++, so it is very powerful. Today let's take a look at its powerful strings.

 

First, let's take these questions to understand and understand the swift strings.

1. How to initialize the swift string?

2. Can the swift string be of a variable length?

3. Is the swift string a pointer or an actual value?

4. What are the features of swift?

 

Next, let me answer these questions one by one.

(1) There are two methods for swift string initialization. The results of both methods are the same. Let's initialize an empty string.

  

  

Var str1 = "" // method 1

  

Var str2 = String () // method 2

When initializing a non-empty string, var str3 = "hello word" can be used. Here, we must note that the value in "" can be any symbol, number, or text, letter ....

 

 

(2) the swift string length is variable. Of course, this is only applicable to strings defined by var. The string constants defined by let are immutable. Because of this feature, it can be combined with other characters, strings through the symbol "=", "+ =" to form a new string.

  

Var str1 = "hello"

Var str2 = "world"

Var str3: Character = "! "// This is the way to define characters

  

  

Var strs1 = str1 + str2 // The result is "hello word"

  

Var strs2 = strs1 + str3 // The result is "hello word !"

 

(3) the swift string is the actual value, not a pointer. When a string variable is assigned to another value, its original value remains unchanged. When a string variable is passed as a parameter, it is also an actual value.

  

Var str1 = "hello"

  

Var str2 = str1

At this time, the value of str1 is "hello". It can be seen that it is not a pointer but an actual value.

(4) What are the features of swift strings? Its Comparison mechanism, verification mechanism, traversal mechanism, conversion mechanism, and combination mechanism are all of its features.

A. Comparison mechanism. The Comparison Between strings can be divided into different types.

When it is completely equal, use "=" to connect;

When the prefix is equal, a string prefix is given, and then the hasPrefix method is called using the Compare string. The parameter is the given prefix;

When the suffix is equal, a string suffix is given, and then the hasSuffix method is called using the ratio string. The parameter is the specified suffix.

 

For example, how many students are there in interest classes in our school? This can be easily used for identification.

 

Let students = ["ios, Xie Ming, s3sj101", "android, Hu Jun, s3jn95", "ios, du meijun, s3jn95", "technical support, Gao Xiaolong, s3jn92 ", "database, Aspen, s3jn92", "database, fengjian, s3j101"]

  

To count the number of people in each interest class, you only need to do this:

Var ios = 0 // defines the number of people in the ios class

Var android = 0 // defines the number of people in the android class

Var technical = 0 // define the number of people in the technical Support class

Var database = 0 // define the number of database classes

 

For student in students {

        

If student. hasPrefix ("ios "){

Ios ++

}

 

If student. hasPrefix ("android "){

Android ++

}

 

If student. hasPrefix ("Technical Support "){

Technica ++

}

 

If student. hasPrefix ("Database "){

Database ++

}

}

      

Print ("Number of ios students \ (ios )")

Print ("Number of android students \ (android )")

Print ("number of technical support students \ (technica )")

Print ("Number of students in the database Class \ (database )")

 

If we want to count the number of people in each original class, we can do this:

 

Let students = ["ios, Xie Ming, s3sj101", "android, Hu Jun, s3jn95", "ios, du meijun, s3jn95", "technical support, Gao Xiaolong, s3jn92 ", "database, Aspen, s3jn92", "database, fengjian, s3j101"]

  

     

Var s3sj95 = 0 // defines the number of students in the s3sj95 class

Var s3jn92 = 0 // defines the number of students in s3jn92

Var s3j101 = 0 // defines the number of students in s3j101

  

 

For student in students {

        

If student. hasSuffix ("s3sj95 "){

S3sj95 ++

}

 

If student. hasSuffix ("s3jn92 "){

S3jn92 ++

}

 

If student. hasSuffix ("s3j101 "){

S3j101 ++

}

}

      

Print ("s3sj95 students \ (s3sj95 )")

Print ("Number of students in s3jn92 \ (s3jn92 )")

Print ("s3j101 students \ (s3j101 )")

 

B: verification mechanism. To determine whether a string is null, we can use the isEmpty attribute to obtain

 

Var str = ""

 

If str. isEmpty {

Print ("str is a Null String ")

} Else {

Print ("str is a non-empty string ")

}

 

Execution result: str is a null string.

 

C: traversal mechanism. We know that a string is composed of characters, and swift can traverse each character in the string.

  

Var str = "helloWord"

 

For char in sre {

Print ("\ (char )")

}

  

The execution result traverses each character.

 

D: conversion mechanism. It also supports string case conversion. The uppercaseString and lowercaseString attributes can be used. Let's take a look:

 

Var str = "helloWord"

  

Var upper = "" // defines a variable for storing the data converted to uppercase.

Var lower = "" // defines a variable that is stored in lowercase.

  

Upper = str. uppercaseString

Lower = str. lowercaseString

  

Then output the data separately. It's easy...

  

  

E: Combination Mechanism. The combination mechanism is a highlight of it, and its length is variable, so it can be combined. This brings great traversal to our development and is a good news for our developers.

  

Here is not an example. They can generate a new string through "+", "+ =", or a valid expression. Finally, it can calculate the total length of a string,

The required method is countElements. The parameter is the string to be calculated.

 

  

Now, today's sharing is here. I hope I can help my friends who have just learned ios or want to learn ios. I am also a newbie. If there are any shortcomings or mistakes, please correct me. Let's make progress together! Everyone can follow my weibo http://www.cnblogs.com/lb0129/

 

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.