1

Here's what I am trying to do :

let courseName = "Bachelor of Tourism Administration(B.T.A)".condensedWhitespace
let upperCaseCourseName = courseName.uppercaseString

let extrctCourseName = upperCaseCourseName.componentsSeparatedByString(" ").reduce("") { $0.0 + String($0.1.characters.first!) }

let upperCasecourseFirstCharcters = extrctCourseName

print(upperCasecourseFirstCharcters) // output : "BOTA" but i want "BTA"

as you see that my outPut of "Bachelor of Tourism Administration(B.T.A)" is BOTA but the desired output is BTA because word of is starting from a lowerCase and i want to ignore that word in my this method , how am gonna do that any idea ?

CodeWalker
  • 2,281
  • 4
  • 23
  • 50
remy boys
  • 2,928
  • 5
  • 36
  • 65
  • 1
    I am not into the Swift language, but I can help you with a logic. Replace "of" with "" and then continue with what you are doing! – CodeWalker Mar 02 '16 at 05:00
  • @LancePreston hey thanks man , let me try this – remy boys Mar 02 '16 at 05:01
  • Go ahead! Should work. – CodeWalker Mar 02 '16 at 05:14
  • well wasn't sure how to replace that in swift so i got a another approach `let extrctCourseName = courseName.componentsSeparatedByString(" ").reduce(""){ let firctChar = String($0.1.characters.first!) return $0.0 + ((firctChar == firctChar.uppercaseString) ? firctChar : "") }` and its working fine :) – remy boys Mar 02 '16 at 05:15
  • Possible duplicate of [How can I identify uppercase and lowercase characters in a string with swift?](http://stackoverflow.com/questions/24268690/how-can-i-identify-uppercase-and-lowercase-characters-in-a-string-with-swift) – Yagnesh Dobariya Mar 02 '16 at 05:25
  • brother you better take some language classes , my question is ignoring the lowercase string not identifying @yagneshdobariya – remy boys Mar 02 '16 at 05:30
  • 1
    pls check the given answer. May it help u – Yagnesh Dobariya Mar 02 '16 at 05:38

3 Answers3

2
let courseName = "Bachelor of Tourism Administration(B.T.A)" //.condensedWhitespace
        var newString = ""
        let array : NSArray = courseName.componentsSeparatedByString(" ")
        for chr in array {
            let str = chr as! NSString
            if str.lowercaseString != str{
                if newString.characters.count > 0{
                    newString = newString.stringByAppendingString(" "+(str as String))
                    continue
                }
                newString = newString.stringByAppendingString((str as String))
            }
        }

        let upperCaseCourseName = newString.uppercaseString
        let extrctCourseName = upperCaseCourseName.componentsSeparatedByString(" ").reduce("") { $0.0 + String($0.1.characters.first!) }
        let upperCasecourseFirstCharcters = extrctCourseName
        print(upperCasecourseFirstCharcters)

//This will defiantly meet to your problem/. Let me know if it works for u or not

Yagnesh Dobariya
  • 2,241
  • 19
  • 29
1

A clean approach would be:

extension Character
{
  public func isUpper() -> Bool
  {
    let characterString = String(self)
    return (characterString == characterString.uppercaseString) && (characterString != characterString.lowercaseString)
  }
}


let courseName = "Bachelor of Tourism Administration(B.T.A)"
let upperCaseCourseName = courseName

let extrctCourseName = upperCaseCourseName.componentsSeparatedByString(" ").reduce("") {
  if($0.1.characters.first!.isUpper()) {
    return $0.0 + String($0.1.characters.first!)
  }else {
    return $0.0
  }

}
Shripada
  • 6,296
  • 1
  • 30
  • 30
1

You can paste this into a playground:

extension String {

    func array() -> [String] {
        return self.componentsSeparatedByString(" ")
    }

    func abbreviate() -> String {
        var output = ""
        let array = self.array()

        for word in array {
            let index = word.startIndex.advancedBy(0)
            let str = String(word[index])

            if str.lowercaseString != str {
                output += str
            }
        }
        return output
    }
}


let courseName = "Bachelor of Tourism Administration(B.T.A)".abbreviate()
print(courseName) // prints BTA
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135