chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

Template & Format string comparison in JavaScript(ES2015)/Python/Swift/Objective-c

2017-07-14

This articles presents all the formatted string in JS, Python, Swift and Objective-C.

JavaScript Example:

  1. variable a can be any type
  2. using `` instead of "" or ''.
  3. `` can be also used for multiline, see following example
// template string
let a = 100;
let b = `${a}`;

// multiple lines
let a = `

line 1

line 2
`

Objective-C

// template string
NSInteger a = 100;
NSString *b = [NSString stringWithFormat:@"%@", a];

// multiple lines
NSString *a = @"line1 \
                line2";

Swift

  • a can be any type
// template string
let a = 100
let b = "\(a)"

// ONLY in swift 4: multiple lines
let longString = """
When you write a string that spans multiple
lines make sure you start its content on a
line all of its own, and end it with three
quotes also on a line of their own.
Multi-line strings also let you write "quote marks"
freely inside your strings, which is great!
"""

Python

  • a can be any type
// template string
a= 100
b= "{}".format(a)

// multiple lines
a = """ this is a very
        long string if I had the
        energy to type more and more ..."""

####Reference
http://www.arkilis.me

arkilis

Make Your Comments