CHGenerator/WebsiteGenerator/Extensions/String+Extensions.swift
Christoph Hagen 14b935249f First version
2022-08-16 10:39:05 +02:00

40 lines
1.1 KiB
Swift

import Foundation
extension String {
var nonEmpty: String? {
self.isEmpty ? nil : self
}
var trimmed: String {
trimmingCharacters(in: .whitespacesAndNewlines)
}
func indented(by indentation: String) -> String {
components(separatedBy: "\n").joined(separator: "\n" + indentation)
}
var withoutEmptyLines: String {
components(separatedBy: "\n")
.filter { !$0.trimmed.isEmpty }
.joined(separator: "\n")
}
func dropAfterLast(_ separator: String) -> String {
components(separatedBy: separator).dropLast().joined(separator: separator)
}
func dropBeforeFirst(_ separator: String) -> String {
components(separatedBy: separator).dropFirst().joined(separator: separator)
}
func lastComponentAfter(_ separator: String) -> String {
components(separatedBy: separator).last!
}
func insert(_ content: String, beforeLast separator: String) -> String {
let parts = components(separatedBy: separator)
return parts.dropLast().joined(separator: separator) + content + separator + parts.last!
}
}