Strings
1️⃣ Declaration
Javascript
// Both Single and Double quotes are OK to create strings in JS
var string1 = "Hello Js!!" ✅
var string2 = 'Hello Js!!' ✅
Swift
// Only Double quotes are OK to create strings in JS
var string1 = "Hello Js!!" ✅
var string2 = 'Hello Js!!' ❌
2️⃣ Length
Javascript
"Hello world!!".length // 13 (counts empty spaces too)
Swift
"Hello world!!".count // 13 (counts empty spaces too)
3️⃣ Case Transformation
Javascript
"Hello world!!".toLowerCase() // hello world!!
"Hello world!!".toUpperCase() // HELLO WORLD!!
Swift
"Hello world!!".lowercased() // hello world!!
"Hello world!!".uppercased() // HELLO WORLD!!
4️⃣ Template literals
Javascript
const name = "Krishna"
const message = `Hello there, i am ${name}.`
Swift
let name = "Krishna"
let message = "Hello there, i am \(name)."
5️⃣ Split
Javascript
var string = "Hello world!!"
string.split("")
Swift
var string = "Hello world!!"
string.split(separator: "")
6️⃣ Multi-line strings
Javascript
// most used way
var string = `
A long time ago
in a galaxy far far away
javascript is being used for coding`
Swift
// """ are always to be alone
var string = """
A long time ago
in a galaxy far far away
Swift is being used for coding
"""
7️⃣ Concatination
Javascript
var string = "Hello"
var string2 = 'world'
var string3 = string + string2 // string.concat(string2)
Swift
var string = "Hello"
var string2 = 'world'
var string3 = string + string2
8️⃣ Typecasting
Javascript
var someNumber = 12345
var asString = String(someNumber) // -OR- someNumber.toString() -OR- `${someNumber}`
Swift
var someNumber = 12345
var asString = String(someNumber) // -OR- '\(someNumber)'
More examples are added on the go
Top comments (0)