Strings
What is a string?
• A sequence of characters.
• Strings can have different character encodings (UTF8,
ASCII)
• Strings in Objective-C, Swift and Java are immutable.
• In Objective-C, Swift and Java strings are interned.
• In C, strings are terminated by null. In Java, Objective-C
and Swift they are not which means their length is stored
internally.
Common operations
• Inserting variables in a string
• Concatenate (adding strings together / append)
• Length
• Contains a string
• Substring Location
• Substring Extraction
• Split
Exercises:
1. Create a string with your name
2. Create a string with ”Hello, “
3. Create an integer and a float with any value you like
4. Create a string that’s a comma-separated list of your
favorite foods.
Insert variables in a string
Java String y = String.format(”x = : %f", x);
String y = ”x = “ + x;
String y = ”x = “ + someObject.toString();
Objective-C NSString *str = [NSString stringWithFomat:@”%@”, object];
%d – integer
%f – float
%@ – calls description method on an object
Swift let str = “x = (variable)”
let str = String(format: “x = %f”, x)
1. Create a string containing “Float: 1.02 Integer:1”
Concatenating strings
Java String y = str1 + str2
String z = new StringBuilder(str1).append(str2).toString();
Objective-C NSString *str= [str1 stringByAppendingString:str2];
Swift let str = str1 + str2
1. Concatenate the “Hello “ string with the string of your name
Length
Java str.length();
Objective-C str.length
Swift str.characters.count
1. Print the length of the string containing your float and integer.
Contains substring
Java str.contains(str2)
Objective-C [str containsString:str2]
Swift str.containsString(str2) //objc method
1. Print “PizzaTime” if your string of favorite foods contains ”pizza”
Substring location
Java str.indexOf(str2) //first character
str.lastIndexOf(str2) //last character (right most occurance)
Objective-C NSRange range = [str1 rangeOfString:@”word"];
Swift str.rangeOfString(str2) //objc method
1. Get the location of a food in your list.
Get Substring
Java str.substring(3,5)
Objective-C [str1 substringWithRange:range]
Swift str1.substringWithRange(range) //objc method
1. Extract one food into a separate string.
Split
Java str.split(“,”);
Objective-C [str componentsSeparatedByString:@”,”]
Hint: NSRange r = NSMakeRange(location, length)
Swift str.componentsSeparatedByString(",")
1. Create an array of your favorite foods. Print the values each on a new line.

Strings and common operations

  • 1.
  • 2.
    What is astring? • A sequence of characters. • Strings can have different character encodings (UTF8, ASCII) • Strings in Objective-C, Swift and Java are immutable. • In Objective-C, Swift and Java strings are interned. • In C, strings are terminated by null. In Java, Objective-C and Swift they are not which means their length is stored internally.
  • 3.
    Common operations • Insertingvariables in a string • Concatenate (adding strings together / append) • Length • Contains a string • Substring Location • Substring Extraction • Split
  • 4.
    Exercises: 1. Create astring with your name 2. Create a string with ”Hello, “ 3. Create an integer and a float with any value you like 4. Create a string that’s a comma-separated list of your favorite foods.
  • 5.
    Insert variables ina string Java String y = String.format(”x = : %f", x); String y = ”x = “ + x; String y = ”x = “ + someObject.toString(); Objective-C NSString *str = [NSString stringWithFomat:@”%@”, object]; %d – integer %f – float %@ – calls description method on an object Swift let str = “x = (variable)” let str = String(format: “x = %f”, x) 1. Create a string containing “Float: 1.02 Integer:1”
  • 6.
    Concatenating strings Java Stringy = str1 + str2 String z = new StringBuilder(str1).append(str2).toString(); Objective-C NSString *str= [str1 stringByAppendingString:str2]; Swift let str = str1 + str2 1. Concatenate the “Hello “ string with the string of your name
  • 7.
    Length Java str.length(); Objective-C str.length Swiftstr.characters.count 1. Print the length of the string containing your float and integer.
  • 8.
    Contains substring Java str.contains(str2) Objective-C[str containsString:str2] Swift str.containsString(str2) //objc method 1. Print “PizzaTime” if your string of favorite foods contains ”pizza”
  • 9.
    Substring location Java str.indexOf(str2)//first character str.lastIndexOf(str2) //last character (right most occurance) Objective-C NSRange range = [str1 rangeOfString:@”word"]; Swift str.rangeOfString(str2) //objc method 1. Get the location of a food in your list.
  • 10.
    Get Substring Java str.substring(3,5) Objective-C[str1 substringWithRange:range] Swift str1.substringWithRange(range) //objc method 1. Extract one food into a separate string.
  • 11.
    Split Java str.split(“,”); Objective-C [strcomponentsSeparatedByString:@”,”] Hint: NSRange r = NSMakeRange(location, length) Swift str.componentsSeparatedByString(",") 1. Create an array of your favorite foods. Print the values each on a new line.