SwiftKit Documentation

Extensions on String

Properties

is​Not​Empty

public var isNotEmpty: Bool  

String that's not empty will be true

non​Empty

public var nonEmpty: String?  

Return nil if string is empty

Methods

with​Prefix​Identation(count:​)

public func withPrefixIdentation(count: Int = 1) -> String  

Add identation in front of string

Example

let name = "Wendy Liga"
let nameWithIdentation = "Wendy Liga".withPrefixIdentation()
     
print(nameWithIdentation) -> "    Wendy Liga"

Parameters

count Int

number of identation

Returns

String with prefix Identation

separate(with:​offset:​)

public func separate(with rightSide: String, offset: Int) -> String  

Separate two text with spesific offset

This function will create table like string

Example:

String         an collection of characters
Int            containing number
Double         bigger than Int

Parameters

with String

right side text

offset Int

how many offset from left side

Returns

String with separation, look Example for mode detail

without​Prefix(_:​replace​With:​)

public func withoutPrefix(_ prefix: String, replaceWith newString: String = "") -> String  

remove prefix on string

Example:

let string = "$10.000"
let stringWithoutPrefix = string.withoutPrefix("$") -> "10.000"
     
or
     
let string = "~/document"
let stringWithoutPrefix = string.withoutPrefix(".") -> "~/document"

Parameters

prefix String

text you want to remove

replace​With String

if you want to replace with spesific string

Returns

String with remove prefix

without​Suffix(_:​replace​With:​)

public func withoutSuffix(_ suffix: String, replaceWith newString: String = "") -> String  

remove suffix on string

Example:

let string = "10.000."
let stringWithoutPrefix = string.withoutPrefix(".") -> "10.000"
     
or
     
let string = "~/document"
let stringWithoutPrefix = string.withoutPrefix(".") -> "~/document"

Parameters

suffix String

text you want to remove

replace​With String

if you want to replace with spesific string

Returns

String with remove suffix

no​White​Space(replace:​)

public func noWhiteSpace(replace with: NoWhiteSpaceReplacer) -> String  

remote white space inside String

with several mode

  • none : will vanish all white space like "This is Amazing" into "ThisIsAmazing"

  • camelCase: remove whitespace and make sure every first character of words seperated with white space will be capitalized like "my name is wendy" into "MyNameIsWendy"

  • snakeCase: replace whitespace with _ like "hello world" into "hello_world"

capitalize​First​Letter()

public func capitalizeFirstLetter() -> String  

Capitalize first letter

Example: string -> String wendy -> Wendy

trim()

public func trim() -> String  

Trim string