To make it more elegant, I would make an extension to the String 3.0 String class with the following code.
extension String
{
public func getAcronyms(separator: String = "") -> String
{
let acronyms = self.components(separatedBy: " ").map({ String($0.characters.first!) }).joined(separator: separator);
return acronyms;
}
}
Afterword you can simply use it as follows:
let acronyms = "Hello world".getAcronyms();
let acronymsWithSeparator = "Hello world".getAcronyms(separator: ".");
let upperAcronymsWithSeparator = "Hello world".getAcronyms(separator: ".").uppercased();
source
share