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 :
let myName        = "Kirit"
let myFullName    = "Kirit Modi"
Initializing String with var keyword:
var myString        = ""               // empty string literal
var anotherString   = String()         // initializer syntax
Append string to another string :
var myName = "Kirit"
myName += "Modi"
Concatenate string :
let hi = "Hi, "
// Concatenate them
var concept = hi + "How are you."
Check if string is Empty :
var emptyCheck = "Hello World!"

if emptyCheck.isEmpty {
    print("Yes, it's empty")
else {
    print("No, it's not empty")
Capitalize all word in string :
let myFullName = "kirit modi"
let b = myFullName.capitalizedString
Check strings are equal :
let 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 :
var 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 :
var 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 :
var myStringName = "KIRIT MODI"

// Lowercase the string
myStringName = myStringName.lowercaseString
Convert s string to Uppercase :
var myStringName = "kirit modi"

// Lowercase the string
myStringName = myStringName.uppercaseString
Check if string contain string :
let myNewName = "Kirit Modi iOS Developer"

// Check if it contains 'iOS'

if myNewName.rangeOfString("iOS") != nil {
    print("Yes it contains 'iOS'")
Generate unique identifier string :
// Generate Unique Identifier

let uniqueString = NSUUID().UUIDString
String whitespace from string :
var spaceString = "    Kirit Modi    "

// Trim all whitespace

spaceString = spaceString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

Split a string by another string :
let spiltString = "Kirit Modi iOS developer"

// Split it by "o"
var new = spiltString.componentsSeparatedByString("iOS")
reverse a string :
let revString = "This is reverse string"

// Reverse its characters
let reversed = String(revString.characters.reverse())
Replace string in string :
let original = "Hit modi"

// Replace "Hit" with "Kirit"

let replace = original.stringByReplacingOccurrencesOfString("Hit", withString:"Kirit")
Get character of string for loop :
var str = "Kirit Modi"

// Loop through its individual characters

str.characters.forEach {
Get length string :
let myString = "Kirit Modi"

// Get the length of the string

let length = myString.characters.count
Get substring using SubstringFromIndex :
let myString = "Kirit Modi"

// Get the substring from position 6
let newString = myString.substringFromIndex(myString.startIndex.advancedBy(6))
Get substring using SubstringToIndex :
let myString = "Kirit Modi"

// Get the substring
let newString = myString.substringToIndex(myString.startIndex.advancedBy(6))

This tutorial, I am trying to include all about String in Swift. But something missing if You know. then please comment.


String in Swift. String in Swift. Reviewed by Kirit Modi on 22:40:00 Rating: 5

No comments:

Powered by Blogger.