String in Swift.
A string is one type of series of the characters in all type of programming language. Swift string is also represented by String data type. The string including the collection of characters. Swift string type is a fast and morden string implementation.
This tutorial provide the definition and useful some of the function of the String data type in swift. we can define string with let or var type.
String Literals :
A string which is collection of Character in sequence by pair which is surround of double quote( " " ). A constant string which is define with keyword let and changing string value at runtime the define String with var keyword.
Initializing String with let keyword :
This tutorial, I am trying to include all about String in Swift. But something missing if You know. then please comment.
Thanks.
This tutorial provide the definition and useful some of the function of the String data type in swift. we can define string with let or var type.
String Literals :
A string which is collection of Character in sequence by pair which is surround of double quote( " " ). A constant string which is define with keyword let and changing string value at runtime the define String with var keyword.
Initializing String with let keyword :
12let myName = "Kirit"
let myFullName = "Kirit Modi"
Initializing String with var keyword:12var myString = "" // empty string literal
var anotherString = String() // initializer syntax
Append string to another string :123var myName = "Kirit"
myName += "Modi"
print(myName)
Concatenate string :1234let hi = "Hi, "
// Concatenate them
var concept = hi + "How are you."
print(concept)
Check if string is Empty :12345678var emptyCheck = "Hello World!"
if emptyCheck.isEmpty {
print("Yes, it's empty")
}
else {
print("No, it's not empty")
}
Capitalize all word in string :123let myFullName = "kirit modi"
let b = myFullName.capitalizedString
print(b)
Check strings are equal :1234567891011let string1 = "Kirit Modi"
let string2 = "Modi Kirit"
// Check they are the same
if string1==string2 {
print("string1 is the same as string2")
}
else {
print("string1 is not the same as string2")
}
Check strings has Prefix :12345678910var name = "Kirit Modi"
// Check if there is a specific Prefix
if name.hasPrefix("Kirit") {
print("Yes, there is")
}
else {
print("No, there isn't")
}
Check strings has Suffix :12345678910var a = "Kirit Modi"
// Check if there is a specific Prefix
if a. hasSuffix("Modi") {
print("Yes, there is")
}
else {
print("No, there isn't")
}
Convert s string to Lowercase :12345var myStringName = "KIRIT MODI"
// Lowercase the string
myStringName = myStringName.lowercaseString
print(myStringName)
Convert s string to Uppercase :12345var myStringName = "kirit modi"
// Lowercase the string
myStringName = myStringName.uppercaseString
print(myStringName)
Check if string contain string :1234567let myNewName = "Kirit Modi iOS Developer"
// Check if it contains 'iOS'
if myNewName.rangeOfString("iOS") != nil {
print("Yes it contains 'iOS'")
}
Generate unique identifier string :1234// Generate Unique Identifier
let uniqueString = NSUUID().UUIDString
print(uniqueString)
String whitespace from string :1234567var spaceString = " Kirit Modi "
// Trim all whitespace
spaceString = spaceString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
print(spaceString)
Split a string by another string :12345let spiltString = "Kirit Modi iOS developer"
// Split it by "o"
var new = spiltString.componentsSeparatedByString("iOS")
print(new)
reverse a string :12345let revString = "This is reverse string"
// Reverse its characters
let reversed = String(revString.characters.reverse())
print(reversed)
Replace string in string :123456let original = "Hit modi"
// Replace "Hit" with "Kirit"
let replace = original.stringByReplacingOccurrencesOfString("Hit", withString:"Kirit")
print(replace)
Get character of string for loop :1234567var str = "Kirit Modi"
// Loop through its individual characters
str.characters.forEach {
print($0)
}
Get length string :123456let myString = "Kirit Modi"
// Get the length of the string
let length = myString.characters.count
print(length)
Get substring using SubstringFromIndex :12345let myString = "Kirit Modi"
// Get the substring from position 6
let newString = myString.substringFromIndex(myString.startIndex.advancedBy(6))
print(newString)
Get substring using SubstringToIndex :12345let myString = "Kirit Modi"
// Get the substring
let newString = myString.substringToIndex(myString.startIndex.advancedBy(6))
print(newString)
This tutorial, I am trying to include all about String in Swift. But something missing if You know. then please comment.
Thanks.
String in Swift.
Reviewed by KIRIT MODI
on
22:40:00
Rating:
