I want this:
var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from a string but have no idea what to do this with this problem.
I want this:
var String1 = "Stack Over Flow" var desiredOutPut = "SOF" // the first Character of each word in a single String (after space) I know how to get the first character from a string but have no idea what to do this with this problem.
You can try this code:
let stringInput = "First Last" let stringInputArr = stringInput.components(separatedBy:" ") var stringNeed = "" for string in stringInputArr { stringNeed += String(string.first!) } print(stringNeed) If have problem with componentsSeparatedByString you can try seperate by character space and continue in array you remove all string empty.
Hope this help!
components(separatedBy:) method. i.e. the original string ends with a whitespace let stringInput = "First Last "To keep it more elegant I would make an extension for the swift 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; } } Afterwords you can just use it like this:
let acronyms = "Hello world".getAcronyms(); //will print: Hw let acronymsWithSeparator = "Hello world".getAcronyms(separator: "."); //will print: H.w let upperAcronymsWithSeparator = "Hello world".getAcronyms(separator: ".").uppercased(); //will print: H.W SWIFT 3
To avoid the crash when there are multiple spaces between words (i.e. John Smith), you can use something like this:
extension String { func acronym() -> String { return self.components(separatedBy: .whitespaces).filter { !$0.isEmpty }.reduce("") { $0.0 + String($0.1.characters.first!) } } } If you want to include newlines as well, just replace .whitespaces with .whitespacesAndNewlines.
Or by using .reduce():
let str = "Stack Over Flow" let desiredOutPut = str .components(separatedBy: " ") .reduce("") { $0 + ($1.first.map(String.init) ?? "") } print(desiredOutPut) var desiredOutPut = String1.components(separatedBy: " ").reduce("") { $0.0 + String($0.1.characters.first!) }You can use the componentsSeparatedByString() method to get an array of strings. Use " " as the separator.
Since you know how to get the first char of a string, now you just do that for each string in the array.
var String1 = "Stack Over Flow" let arr = String1.componentsSeparatedByString(" ") var desiredoutput = "" for str in arr { if let char = str.characters.first { desiredoutput += String(char) } } desiredoutput By the way, the convention for variable names I believe is camel-case with a lowercase letter for the first character, such as "string1" as opposed to "String1"
For the sake of completeness this is a solution with very powerful enumerateSubstrings(in:options:_:
let string = "Stack Over Flow" var result = "" string.enumerateSubstrings(in: string.startIndex..<string.endIndex, options: .byWords) { (substring, _, _, _) in if let substring = substring { result += substring.prefix(1) } } print(result) .byWords option. The enumerated substring. If substringNotRequired is included in opts, this parameter is nil for every execution of the closure. result += substring!.prefix(1) if you want to be extra paranoid result += substring?.prefix(1) ?? ""let inputString = "ABC PQR XYZ" var stringNeed = "" class something { let splits = inputString.components(separatedBy: " ") for string in splits { stringNeed = stringNeed + String(string.first!) } print(stringNeed) }