Swift and JavaScript comparison snippets(3) — Strings and Characters

Written by unbug | Published 2018/06/28
Tech Story Tags: javascript | swift | swift-and-javascript | swift-vs-javascript | strings-and-characters

TLDRvia the TL;DR App

String Literals

Swift

let someString = "Some string literal value"

// Multiline String Literals with three double quotation marks (""")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 ontill you come to the end; then stop.""""

// String Mutabilityvar variableString = "Horse"variableString += " and carriage"// variableString is now "Horse and carriage"

let constantString = "Highlander"constantString += " and another Highlander"// this reports a compile-time error - a constant string cannot be modified

JavaScript

const someString = "Some string literal value"

// Multiline String Literals back-tick (` `)const 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 ontill you come to the end; then stop."`

// String Mutabilitylet variableString = "Horse"variableString += " and carriage"// variableString is now "Horse and carriage"

const constantString = "Highlander"constantString += " and another Highlander"// this reports a compile-time error - a constant string cannot be modified

String Interpolation

Swift

// insert values into the string literal is wrapped in a pair of parentheses, prefixed by a backslash (\)let multiplier = 3let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"// message is "3 times 2.5 is 7.5"

JavaScript

const multiplier = 3const message = `${multiplier} times 2.5 is ${multiplier * 2.5}`// message is "3 times 2.5 is 7.5"

Accessing and Modifying a String

Swift

// String Indiceslet greeting = "Guten Tag!"greeting[greeting.startIndex]// Ggreeting[greeting.index(before: greeting.endIndex)]// !greeting[greeting.index(after: greeting.startIndex)]// u

// Inserting and Removingvar welcome = "hello"welcome.insert("!", at: welcome.endIndex)// welcome now equals "hello!"

welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))// welcome now equals "hello there!"

welcome.remove(at: welcome.index(before: welcome.endIndex))// welcome now equals "hello there"

let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndexwelcome.removeSubrange(range)// welcome now equals "hello"

JavaScript

const greeting = "Guten Tag!"greeting.slice(0, 1)// Ggreeting.slice(-1)// !greeting.substr(1, 1)// u

// Inserting and Removinglet welcome = "hello"welcome += "!"// welcome now equals "hello!"

welcome = welcome.substr(0, welcome.length - 1) + " there" + welcome.slice(-1)// welcome now equals "hello there!"

welcome.substr(0, welcome.length - 1)// welcome now equals "hello there"

let index = welcome.indexOf(' ');welcome.slice(0, index)// welcome now equals "hello"

More of Swift and JavaScript comparison snippets


Published by HackerNoon on 2018/06/28