/* Continents.swift (c) Kari Laitinen http://www.naturalprogramming.com 2016-11-05 File created. 2018-03-17 Last modification. This program shows how some common String methods work. - index() returns an index which is of type String.Index. Swift strings cannot be indexed with values of type Int. - range( of: ) can tell where another string is located within a string - subscripting can take a substring in the specified range. - prefix( upTo: ) returns a substring taken from the beginning. - suffix( from: ) returns a substring from the end of a string. - replacingOccurrences( of:, with: ) replaces substrings. - uppercased() returns a string in which all letters are converted to upper case if necessary. */ import Foundation let indexes = "01234567890123456789012345678901234567890123456" let continent_names = "America Europe Africa Asia Australia Antarctica" print( "\n " + indexes ) print( " " + continent_names + "\n" ) let index_of_9th_character = continent_names.index( continent_names.startIndex, offsetBy: 8 ) print( " \( continent_names[ index_of_9th_character ] )" ) let range_of_first_ia = continent_names.range( of: "ia" ) let index_of_first_ia : Int = continent_names.distance( from: continent_names.startIndex, to: range_of_first_ia!.lowerBound ) print( " \( index_of_first_ia )" ) // It seems that there is no Swift method that would correspond with the // method lastIndexOf() that exists in many programming languages. let range_of_atlantis = continent_names.range( of: "Atlantis" ) if range_of_atlantis == nil { print( " Atlantis is not a known continent." ) } let lower_bound_of_substring = continent_names.index( continent_names.startIndex, offsetBy: 15 ) let upper_bound_of_substring = continent_names.index( continent_names.startIndex, offsetBy: 26 ) let range_of_substring = lower_bound_of_substring ..< upper_bound_of_substring let substring_15_26 = continent_names[ range_of_substring ] print( " " + substring_15_26 ) let substring_0_27 = continent_names.prefix( upTo: continent_names.index( continent_names.startIndex, offsetBy: 27 ) ) print( " " + substring_0_27 ) let substring_from_27_to_end = continent_names.suffix( from: continent_names.index( continent_names.startIndex, offsetBy: 27 ) ) print( " " + substring_from_27_to_end + substring_0_27 ) print( " " + continent_names.replacingOccurrences( of: "ica", with: "XXX" ) ) print( " " + continent_names.uppercased() + "\n" )