Swift Initial-4 operator vs. String manipulation

Source: Internet
Author: User


// Playground - noun: a place where people can play

Import UIKit
//import Foundation

Var str = "Hello, playground"
//swift new operator Nil Coalescing Operator (nil aggregate operator) binary operator It is designed for optional data, usually used in the process of unpacking
// Example: a??b ---> a!=nil? a! : b (a is unpacked, otherwise it returns the default value b) a must be optional, b must be unpacked with a Type is consistent
// instance:
Var name:String?
Name="andy"

// When you need to pay attention to name != nil !!= There is a space before and after, otherwise it will report an error, if there is no space name!
// When there is no space: error: type ‘()‘ does not conform to protocol ‘BooleanType’
If name != nil{
    Println("hello,\(name)")
    // Add the ! sign here, as I said before.
    Println("hello"+String(name!))
}else
{
    Println("hello,kim")
}

// Simplify if code-1:
// Development Tips: Leave a space before and after the element to separate the code or symbol
Let myName:String=name != nil ? name! : "kim"
Println("hello"+myName)

// Simplify if code-2
Let myNames:String=name ?? "kim"
Println("hello"+String(myNames)) //This ensures that myNames is always a meaningful value


// Interval operator Range Operator Commonly used forin loop

// Closed interval [a,b] ---->a...b //three points
// Open before closing [a,b)---->a..<b //two points

For indexs in 1...10
{
    Indexs
 // indexs=10 error:error: cannot assign to ‘let‘ value ‘indexs’ , default constant
}

For indexs in 1..<10
{
    Indexs
    // indexs=10 error:error: cannot assign to ‘let‘ value ‘indexs’ , default constant
}

//Logical Operator
// !a a&&b a||b Note a & b , a | b This is a bit operation, not a logical operation

// String Mutability
// var declares a mutable string, let is immutable
// traversing the string
For c in str
{
    Println(c) //c is Character type
}

// Character type
Var ch:Character="!" //Writing a character will give an error
Str.append(ch)

Println(str)
 // str +=ch // This is not possible, the type does not match, two strings can
countElements(str) / / Calculate the length, you can intelligently treat Chinese as a character (Chinese language is uncommon in the underlying language)
Var str1:NSString=str
Str1.length
Var str2="hello\u{1F496}"
countElements(str2) //length is 6
Var str3:NSString="hello\u{1F496}"
Str3.length // length is 7, here you can find that swift has more advantages than OC when processing strings

Let SFInt:Int = 2
Let SFDouble:Double = 2;
Let SFBool:Bool=true;
Let SFTuple:(Int,Int) = (2,3) //tuple
Let SFOptional:Int?=nil
Let SFCharaceter:Character = "!"

Println("Bool:\(SFBool) \n Tuple:\(SFTuple)") // \n Wrap

//String Comparing
Let stringA="abc"
Let stringB="abc"
stringA==stringB
Let stringC="abd"
Let stringD="d"
stringA<stringC
stringA<stringD

Let array=["abc","abc","abc","abc"]
For strs in array
{ // strs.hasSuffix("c")
    If(strs.hasPrefix("a"))
    {
     Println("String=\(strs)")
    }
}


// The framework imported into OC can use other methods in the original OC.
  stringA.capitalizedString
  stringA
  stringA.uppercaseString
  stringA.lowercaseString
  stringA
  
// trim operation // intercepts the beginning and end of the string, it has no effect on the middle
Var stringE=" a b b c "
stringE.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) //This method comes from OC
stringE
stringE.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString:"a")) // is not starting from scratch, can't intercept
stringE.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString:" a")) //Note is two characters, a precedes a space
// split
stringE.componentsSeparatedByString(" ")

stringE.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString:"b"))

//join
Var stringF="-"
stringF.join(["1","2","3"])

// range
stringE.rangeOfString("b") //Returns an optional type, returns the first position
// The latest swift syntax Perhaps rangeOfString returns a tuple (22, 4), no longer a range of 23..<27, which is provided by the netizen and has not been verified!!!!!!!!!!!
// reverse acquisition
stringE.rangeOfString("b", options: NSStringCompareOptions.BackwardsSearch)

// ignore case
stringE.rangeOfString("b", options: NSStringCompareOptions.CaseInsensitiveSearch)

// String type has a String.Index index
stringE.startIndex
stringE.endIndex

// < > is the concept of generics in swift
Let stringRange = Range<String.Index>(start:stringE.startIndex,end:stringE.endIndex)

Let startIndex=str.startIndex // Note that its 0 is not an integer 0, but a String.Index type.
// Integer 10 means 10 steps backwards
Let endIndex:String.Index=advance(stringE.startIndex, 10)
Let seachIndex=Range<String.Index>(start: startIndex, end: endIndex)

// Search within a fixed range
stringE.rangeOfString("b", options:NSStringCompareOptions.CaseInsensitiveSearch, range: seachIndex)

// subString
Var toIndex=advance(startIndex, 10)
stringE.substringToIndex(toIndex)
Var fromIndex=advance(startIndex,13)
stringE.substringFromIndex(fromIndex)
// returned from 10 - 13 does not include the 13th character
stringE.substringWithRange(Range<String.Index>(start: toIndex, end: fromIndex))

//insert
Var insertIndex=advance(stringE.startIndex,5)
stringE.substringFromIndex(fromIndex)
//stringE.insert("!", atIndex: insertIndex)
// stringE.removeAtIndex(insertIndex)
// stringE.replaceRange(<#subRange: Range<String.Index>#>, with: <#C#>)
// stringE.removeRange(<#subRange: Range<String.Index>#>) 



Swift Initial-4 operator vs. String manipulation


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.