C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Swift String
Use strings and string literals. See a list of string methods that can be called.
String. A cloud of mist gathers in the forest. Now it forms the letters of a word. You glimpse this mysterious string data for just a few moments.
String, methods. The forest has many secrets. It is strange. In Swift we have strings and these must be accessed with methods and indexes (not ints).appendcapitalizedcharacterscomponentscontainsendIndexhasPrefixhasSuffixindexinsertisEmptylowercasedpaddingrangeremoveSubrangereplaceSubrangereplacingOccurrencesreplacingCharactersreserveCapacitystartIndexString()unicodeScalarsuppercasedutf16utf8write+= (Append)+ (Concatenate)"\(x)" (Interpolation)"" (Literal)
Length. The length of a string is returned by the endIndex property. This not an Int. Instead it is an index, so we can use it to access or change other chars in a string.endIndex: Length
Substring. With a range we address a group of characters in a string. To take a substring, we create a range (with advance, startIndex, endIndex) and then access it.Substring
Convert to Int. With toInt() we convert a string to an Int. This returns an optional Int, which we can access in an "if let" statement.Convert
Trim. In Foundation, we find a method that can trim characters from the start and end of a string. We pass a set of characters, and these are removed.Trim
Padding. In Foundation, we can apply padding (of any string) to an existing string. This adds characters to a string until a certain length is met.Padding
Reverse. No built-in method reverses a string. But with an extension method, we can call reverse() on a string to invert the ordering of the string's characters.Reverse String
Ciphers. A cipher translates the characters of a string according to some logic. For example, a Caesar cipher shifts characters, wrapping values when needed.Cipher: CaesarCipher: ROT13
ASCII table. The indexes 0 through 127 are used to represent ASCII characters. Swift provides some helpful methods (on UnicodeScalar) for handling ASCII.ASCII Table
Many features. Strings are used in complex ways. It makes sense that there are many ways to test and change them. Some familiarity makes this easier.
© TheDeveloperBlog.com