String Manipulation in Swift Language
Hello there !!! Welcome to my post about string manipulation in swift.The current post covers the latest swift 4 syntax as specified in the apple developer site. This post will show you all the necessary operations that we need to do at sometime while programming in swift. I hope you will find this post informative and useful. Happy coding :)
String Declaration:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let constantString = "This is a single line string" | |
//now a multiline string | |
let quotation = """ | |
The White Rabbit put on his spectacles. "Where shall I begin, | |
please your Majesty?" he asked. | |
"Begin at the beginning," the King said gravely, "and go on | |
till you come to the end; then stop." | |
""" |
String Concatenation:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var firstString = "Hello Everyone." | |
var secondString = "This is Nikesh's Blog" | |
var newString = firstString + secondString | |
print(newString) | |
//OUTPUT: Hello Everyone.This is Nikesh's Blog |
Working with characters and substrings:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var someString = "Hello there" | |
//Create a string using array of characters | |
let charArray: [Character] = ["N","I","K","E", "S", "H"] | |
let srtingFromArray = String(charArray) // "NIKESH" | |
//Get array of characters from a string | |
var charatctesFromString = Array(someString) // ["H", "e", "l", "l", "o", " ", "t", "h", "e", "r", "e"] | |
print(charatctesFromString) | |
//characters count in string | |
var numberOfCharacters = someString.count | |
//print all the characters in a string | |
for char in someString{ | |
print(char) | |
} | |
//H | |
//e | |
//l | |
//l | |
//o | |
// | |
//t | |
//h | |
//e | |
//r | |
//e | |
//Access characters at different index | |
var firstCharacter = someString.first | |
var lastCharacter = someString.last | |
//get character at nth index | |
func getCharAt(index: Int, string: String) -> Character { | |
let index = string.index (string.startIndex, offsetBy: index) | |
return string[index] | |
} | |
var char1 = getCharAt(index: 6, string: "Apple Inc") // I | |
//get substring bty specifying and start and end indices | |
func substring(string: String, fromIndex: Int, toIndex: Int) -> String? { | |
if fromIndex < toIndex && toIndex < string.count /*use string.characters.count for swift3*/{ | |
let startIndex = string.index(string.startIndex, offsetBy: fromIndex) | |
let endIndex = string.index(string.startIndex, offsetBy: toIndex) | |
return String(string[startIndex..<endIndex]) | |
}else{ | |
return nil | |
} | |
} | |
substring(string: "Hello World", fromIndex: 0, toIndex: 3)! // Hel | |
substring(string: "Hello World", fromIndex: 0, toIndex: 20) // nil | |
Strings Comparison:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let quotation = "Hello World" | |
var sameQuotation = "Hello World" | |
if quotation == sameQuotation { | |
print("These two strings are considered equal") | |
} | |
// Prints "These two strings are considered equal" |
Case Conversion in strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var str = "hello Nikesh" | |
//Capitalize first letter of every word | |
var capitalizedStr = str.capitalized // "Hello Nikesh" | |
//Change string to uppercase | |
str.uppercased() // "HELLO NIKESH" | |
//change string to lowercase | |
str.lowercased() // "hello nikesh" |
Comments
Post a Comment