Swift's String and character types provide a fast, Unicode- complaint way to work with text in our code. The syntax for string creation and manipulation is lightweight and readable, with a string literal syntax that is similar to C.
Swift's String type is a fast, modern string implementation. Every string is composed of encoding- independent. Unicode characters, and provides support for accessing those characters in various Unicode representations.
Swift’s String type is bridged with Foundation’s NSString class. If you are working with the Foundation framework in Cocoa, the entire NSString API is available to call on any String value you create when type cast to NSString, as described in AnyObject. You can also use a String value with any API that requires anNSString instance.
1) To create empty string literal:
var emptyString = ""
2) To initialize a string use the initializer syntax:
var strName = String()
3) For example:
var strName = "Jitendra"
if strName. isEmpty {
println("string is empty")
}
else{
println("string is not empty")
}
Swift's String type is a value type. If you create a new String value, that String value is copied when it is passed to a function or method, or when it is assigned to a constant or variable. In each case, a new copy of the existing String values is created, and the new copy is passed or assigned, not the original version.
4) Swift’s String type represents a collection of Character values in a specified order. You can access the individual Character values in a string by iterating over that string with a for-in loop.
example:
for character in strName{
println(character)
}
5) To retrieve a count of the Character values in a string, call the global count(_:) function and pass in a string as the function’s sole parameter.
6) String values can be added together with the addition with the addition operator(+) to create a new String value :
let firstName = "Jitendra"
let lastName = "Solanki"
var fullName = firstName + lastName
Working with index:
Each String value has an associated index type, String.Index, which corresponds to the positions of each Character it contains.
As mentioned above, different characters can require different amounts of memory to store, so in order to determine which Character is at a particular position, you must iterate over each Unicode scalar from the start or end of that String. For this reason, Swift strings cannot be indexed by integer values.
7) Use the startIndex property to access the position of the first Character of a String, and the endIndexproperty to access the posision of the last.
8) A String.Index value can access its immediately preceding index by calling the predecessor() method, and its immediately succeeding index by calling the successor() method.
9) Use the global function indicies(_:) to create a Range of all of the indexes used to access individual characters in a string.
Inserting and Removing :
10) To insert a character into a string at a specified index, use the insert(_:atIndex:) method.
- var welcome = "hello"
- welcome.insert("!", atIndex: welcome.endIndex)
11) To insert another string at a specified index, use the splice(_:atIndex:) method.
- welcome.splice(" there", atIndex: welcome.endIndex.predecessor()
12) To remove a character from a string at a specified in index, use the removeAtIndex(_:)method.
13) To remove a substring at a specified range, use the removeRange(_:)method:
example:
let range = advance(fullName.endIndex, -6)..<fullName.endIndex
fullName.removeRange(range)
Comparing Strings:
Swift provides three ways to compare textual values: string and character equality, prefix equality, and suffix equality.
14) String and character equality is checked with the “equal to” operator (==) and the “not equal to” operator (!=),
Prefix and Suffix Equality:
15) To check whether a string has a particular string prefix or suffix, call the string’s hasPrefix(_:) and hasSuffix(_:) methods, both of which take a single argument of type String and return a Boolean value.
No comments:
Post a Comment