String and Characters

string is a series of characters, such as "hello, world" or "albatross". Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values.

String Literals

You can include predefined String values within your code as string literals. A string literal is a fixed sequence of textual characters surrounded by a pair of double quotes ("").

Use a string literal as an initial value for a constant or variable:

 let a=”Hello”
 swift automatically infer the type of variable a and assign it with type of literal “Hello”
empty string is created like this..
       var emptyString = “”
      or var x=String()
if you want to check that string is empty then there is a method isEmpty. Have look on following code
      var a=String()
      var b=”Helllo”
      if a.isEmpty {
                       print(“String is empty”)
     if b.isEmpty {
     print(“String is not empty”)

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *