Skip to content

Instantly share code, notes, and snippets.

@artemkrachulov
Created July 21, 2016 10:15
Show Gist options
  • Save artemkrachulov/3c271cb7c813bf453a70a506910d355b to your computer and use it in GitHub Desktop.
Save artemkrachulov/3c271cb7c813bf453a70a506910d355b to your computer and use it in GitHub Desktop.
Uppercase first character for represented string.
//
// String+Case.swift
//
// Created by Artem Krachulov
// Copyright (c) 2016 Artem Krachulov. All rights reserved.
// http://www.artemkrachulov.com
//
import UIKit
extension String {
/// Uppercase first character for represented string.
///
/// Usage:
///
/// "hello, world".uppercaseFirst // "Hello, world"
/// "".uppercaseFirst // ""
public var uppercaseFirst: String {
guard !isEmpty else { return "" }
var text = self
text.replaceRange(text.startIndex...text.startIndex, with: String(text[text.startIndex]).capitalizedString)
return text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment