As always, when new version of Swift comes out, I read through the manual and generate questions for review. So here we go with Strings. As you know, Strings are once again Collection types, so no more .characters, for which we may all be thankful.
How do you create a multiline String?
Using the triple-quote
let x = """
he said she said
and then more
and then still more
Notice, green recruit, that it’s the BOTTOM triple quote that determines where indentation starts. You can add indents, you an add \ to join a line together.
When you get a Substring from a String, what’s the type? String?
No, gentle soul, it is not String. It is SubString.
What protocol do String and Substring share?
They share StringProtocol.
Oh! Is that why string manipulation types so often take StringProtocol types as input?
Just as you say. You are wiser than you look.
It’s so good to know that copy-by-default ensures that a String, as genuine 14 karat Value type, is ALWAYS copied behind the scenes.
Did you see what I did there? It’s not always copied behind the scenes. It’s often passed by reference and a copy is only performed if you’re about to mutate the String. You get mucho performance gains because of this evil little trick. A sleight of hand, if you will. A bit of the ol’ razzle-dazzle three card monte.
Why is it evil?
It’s not really evil.
Wow, you can append a character to a String! Just use the + like you to do concatenate?
No, that would be too obvious. You have to use the
What is string interpercolationaltion?
I think you mean string interpolation. It just means plugging one string into another using variables. Just like with C and other languages.
Can you create a String from an array of characters?
You sure can, sweet cheeks. Just cast that sucker into a String and hey presto.
Are you harrassing me?
Do you want me to? Would you like me to put up a trigger warning so you don’t get a rash or something?
You have very poor manners.
I do. I spend long winter nights grieving about them.
What is an extended grapheme cluster?
Well you managed *that* one all right. It’s a sequence of one or more unicode characters that results in a single, that means one and one only, human readable character.
So do I iterate over every Unicode scalar to count human readable characters in a string?
Well, sort of. YOU don’t but the .count property does.
So does count return a count of Unicode scalars?
NO. It returns a count of human readable values, but to do that it has to read over every single blessed scalar in the String, so THINK before you use it, okay? Use your head for something besides a hatrack.
It’s such a relief to know that String.count always == [NSString length]
Only in your dreams ,sweet pea. I can think of other forms of relief that are faster and more efficient. NSString length doesn’t count human readable values, it counts 16 bit code units in a UTF 16 string representation.
My God, how do you get away with being so nasty?
Who says I get away with it?
Well at least .index doesn’t have to read over every Unicode scalar in a string?
You really live in your own little universe, don’t you? And you don’t listen. Index also has to iterate over every Unicode scalar in a string.
And even so, you can still use integer values as indexes into a String! That’s marvelous!
NO YOU CAN’T. An index doesn’t translate into a physical position in an array. No integers.
So startIndex gets the first position in a String?
It does. I can see your mistake driving at me like a mack truck. Here it comes.
So endIndex gets the last position in a String?
And you mowed down Granny, who was just quietly trying to cross the road to by something at the fruitstand. NO. endIndex gets the position AFTER the last character in the String.
Here’s the idiom for getting the last character in a string.
let i2 = message.index(before: message.endIndex)
Awfully sporting of you!
Oh, isn’t it JUST? Let’s have a highball at Wimbledon season next. I’ll look for you.
If a string is empty, what happens to startIndex and endIndex?
At long last they have found the dream of brotherhood and true equality. They’re equal.
Remember before:, after: and offsetBy: when using Index. They are your friends, if it’s possible for you to have friends.
Since String is now a collection, well, let’s just say, it gives one certain thoughts, even a frisson!
Oh it does, it does! You can use before, after and offsetBy on any Collection type, like Array, Set or Dictionary.
How do you insert and remove things in a String?
Just as you say. You use insert and remove. If you’re inserting a character, just pass in the character. If you’re inserting a String you have to use a contentsOf: label.
Just to make sure two strings are brothers from another mother, how can you check their full equality qua strings?
Qua? Showing off the schoolboy Latin? Poser. You can test as you would expect with == and !=.
Canamagonically equivalent. It’s one of my favorite phrases but I don’t know what it means?
There’s a lot more than that you don’t know, oh wizard of language. The phrase is canonically equivalent. It means a string may be represented in two cases by different underlying Unicode BUT when translated to human readable, the underlying Unicode don’t make no never mind. The humans see the same letters and marks that mean so very much to them. So very, very much.
How can I quickly check the beginnings and endings of strings for other strings?
If I were you I’d just ask someone. But if you really feel like you have to use Swift, you can use hasPrefix and hasSuffix. I have no doubt you will make a raging success of it.
I can’t access the underlying Unicode?
In your case it’s more a case of ‘I wouldn’t.’ or ‘someone should stop you.’ I can’t imagine what you would want to do with it that was within your abilities. However, if you just can’t resist, you can use the utf8, utf16 and utfScalars properities. The last one returns a 21-bit unit that’s equivalent to UTF32. That will be enough to get you into the sort of trouble you probably won’t get out of. But the nice thing is, you probably will never know it.
What do you say to a recruiter when they proudly tell you they’re looking for a rock star with passion?
I can’t print what I tell them here. DO NOT HIT THEM, it will get you in trouble. Tell them this: it’s a job. It doesn’t require passion. It requires competence, insight, intelligence, discipline, confidence and ability. Passion is what you need if you’re not very good and you want people not to notice.