Kinderswift 9: Strings


Hello, welcome to Kinderswift Lesson 9. I’m Steve from

So far we’ve worked with numbers and colors, but what if you wanted to work with text?

The basic text type in Swift is String.

Like a literal Double of 3.5, we can have a literal String. Enclose String literals in quotes like this

"This is a string"

We declare strings the same way we declare other types we’ve discussed. Implicit typing works on strings. You can declare a string constant by typing this:

let string1 = "stringthing1"

You can declare a string variable by typing this:

var string3 = "blank"

You can of course be explicit about your type as well:

let string2:String = "my Double result is"

You can also make an empty string by declaring this.

var myString:String = ""

If you have two strings and you want to join them together we concatenate them. The concatenation operator may look a little familiar: it is the plus symbol.

A simple example would be this:

string3 = string2 + string3

Notice what happens. Swift joins the two strings but there is no space between them. Swift will take what you give it and put them together. Often we will concatenate a string literal with a space between two strings in a concatenation like this:

string3 = string1 + " " + string2 //concatenation

If you are adding to the end of a string the += is an operator for that.

string3 += " " + string1 //concatenation

It is important to remember this is not addition even though it uses a plus.

There are a lot of string functions. We will cover many of them as we go through the lessons. There is one initializer function you should get used to early.

String(format:"hello pizza number %i",12)

This is the formatting string. It takes numbers and other strings, usually in variables, and adds them correctly formatted to the string. We use this very often when displaying formatted numbers.
The key is the part behind the % sign. It adds a code to say “add this type” to the string. %i is for Int. There is a code for Double too %f. We add a set of numbers before it to give the length in characters and number of decimal points. %6.2f adds six size spaces before with two decimal points. %10.3f is a ten space number with three decimal points

Try this:

let a = 2.567891
let pi = 3.1415926
let myInt = 12
String(format:"the integer i equals %i",myInt)
String(format:"a = %6.2f",a)
String(format:"the value of pi is %6.3f",pi)

Let’s do an example of a variable and a calculation:

String(format:("The area of radius %1.3f is %2.3",a, pi * a * a)

Now that we have done this last example, Let’s go back to our pizzaArea and deep dish volume functions

func printPizzaArea(diameter:Double){
	let radius:Double = diameter / 2.0
	let area = pizzaArea(radius)
	println (radius,area)

Right now they print pretty ugly. Let’s pretty it up a bit using strings.

func printPizzaArea(diameter:Double){
	let radius:Double = diameter / 2.0
	let area = pizzaArea(radius)
	let displayString = String(format:"A Pizza with radius %2.2 is %2.2",value:)
	println (displayString)

The print gives us six digits of space for the format, and two decimal places. We print the string instead of the values.

Next time, we’ll collect variables and functions into a single object called a class.

For a challenge, try to do the following:

Make a function conctenateWithSpace(a,b) that returns a string that concatenates a with b, but with a space between therm. so concantenateWithSpace("Hello","Pizza") returns a string “Hello Pizza”

Format the string in deep dish so it prints this.
For a pizza of diameter 12
The area is x.xx and the volume is x.xx

hint: you can use more then one printLn in a function.

One thought on “Kinderswift 9: Strings”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s