Powerful Swift string

Source: Internet
Author: User
Tags ming

Swift sets the length of the Hundred family, absorbing the mainstream language java,c,c++ and other good features, so it is very powerful, today we will look at its powerful string.

First, we take a few questions to understand and understand Swift's string.

1, how to initialize the Swift string?

2. Is swift string length variable?

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

4. What are the features of Swift?

Here, let me answer these questions.

(1) There are 2 ways to initialize swift strings, both of which result in the same way. Let's initialize an empty string.

  

  

var str1 = ""//The first way

  

var str2 = String ()//second type

When initializing a non-empty string, you can do so var str3 = "Hello word", here to illustrate, "" in the value can be any symbol, number, text, letters ....

(2) Swift string length is variable, and of course this is only for strings defined with Var, and the string constants defined with let are immutable. Because of this feature, it can be combined with other characters, the string is synthesized by the symbol "=", "+ =" Group of the new string.

  

var str1 = "Hello"

var str2 = "World"

var str3:character = "!" This is how the characters are defined

  

  

var strs1 = str1 + str2//result is "Hello word"

  

var strs2 = strs1 + STR3//result is "Hello word!"

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

  

var str1 = "Hello"

  

var str2 = str1

At this point, the value of str1 is still "hello", thus, it is not a pointer, is an actual value.

(4) What are the characteristics of swift string? Its comparison mechanism, verification mechanism, traversal mechanism, transformation mechanism, and combination mechanism are all of its characteristics.

A, comparison mechanism. Comparisons between strings can be varied.

When it is quite equal, it is connected with "= =";

It compares the prefix equal, given a string prefix, and then uses the string to be compared, call the Hasprefix method, the parameter is given the prefix;

It compares suffixes when given a string suffix, and then calls the Hassuffix method with the string, with the argument given as the suffix.

For example, our school in the interest class, to count the number of different classes of interest how many? With this it can be easily found.

Let students =["iOS, Ming Xie, s3sj101", "Android, Hu June, S3jn95", "iOS, All-June, S3jn95", "technical support, Gao Xiaolong, s3jn92", "Database, Aspen, s3jn92", "Database, sword-sealing, S3j101 "]

  

To count the number of individual interest classes, just do this:

var iOS = 0//define the number of iOS classes

var android = 0//Define the number of Android classes

var technical = 0//define number of technical support classes

var database = 0//define the number of DB 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 classes (iOS)")

Print ("The number of Android classes reported" (Android))

Print ("Number of technical support classes \ (Technica)")

Print ("Number of database Classes")

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

Let students =["iOS, Ming Xie, s3sj101", "Android, Hu June, S3jn95", "iOS, All-June, S3jn95", "technical support, Gao Xiaolong, s3jn92", "Database, Aspen, s3jn92", "Database, sword-sealing, S3j101 "]

  

     

var s3sj95 = 0//define number of s3sj95 classes

var s3jn92 = 0//define number of s3jn92 classes

var s3j101 = 0//define number of s3j101 classes

  

For student in students {

        

If Student.hassuffix ("s3sj95") {

s3sj95++

}

If Student.hassuffix ("S3jn92") {

s3jn92++

}

If Student.hassuffix ("s3j101") {

s3j101++

}

}

      

Print ("Number of s3sj95 classes \ (s3sj95)")

Print ("Number of s3jn92 classes \ (s3jn92)")

Print ("Number of s3j101 classes \ (s3j101)")

B: Authentication mechanism. Judging if a string is not empty, we can use the IsEmpty property to get

var str = ""

If Str.isempty {

Print ("Str is an empty string")

}else{

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

}

The result of the execution is: STR is an empty string

C: Traversal mechanism. We know that strings are composed of characters, and Swift can iterate through each character in the string.

  

var str = "Helloword"

For Char in SRE {

Print ("\ (char)")

}

  

The execution result will iterate through each character.

D: conversion mechanism. It also supports casing conversions for strings. Through the uppercasestring and LowerCaseString properties can do, let's take a look at:

var str = "Helloword"

  

var upper = ""/define a variable to be converted to uppercase

var lower = ""////defines a variable that is converted to lowercase

  

Upper = Str.uppercasestring

Lower = Str.lowercasestring

  

Then output separately, very simple ...

  

  

E: combination mechanism. The combination mechanism is a big bright spot, its length is variable, so it can be combined. This brings great traversal to our development and is the Gospel of our developers.

  

This is not an example where they can generate new strings between "+", "+ =" and a valid expression. Finally, it can also calculate the total length of the string,

The method you need to use is: countelements. parameter is the string to evaluate itself.

  

Well, today's share is here. Hope can help just learn iOS or want to learn iOS friends, I am also a novice, there is insufficient and wrong place to ask the big God. Let's make progress together! You can follow my Weibo http://www.cnblogs.com/lb0129/

Powerful Swift string

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.