-
Notifications
You must be signed in to change notification settings - Fork 1
/
Contents.swift
56 lines (45 loc) · 980 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import UIKit
// Challenge step 1
extension String {
func withPrefix(_ prefix: String) -> String {
guard !self.hasPrefix(prefix) else { return self }
return String("\(prefix)\(self)")
}
}
let test = "pet"
let test2 = "carpet"
let test3 = "pet2"
let test4 = "car"
test.withPrefix("car")
test2.withPrefix("car")
test3.withPrefix("car")
test4.withPrefix("car")
// Challenge step 2
extension String {
func isNumeric() -> Bool {
guard (Double(self) != nil) else { return false }
return true
}
}
let number = "123"
let number2 = "A123"
let number3 = "1.23"
let number4 = "1"
number.isNumeric()
number2.isNumeric()
number3.isNumeric()
number4.isNumeric()
// Challenge step 3
extension String {
func lines() -> [String] {
return self.components(separatedBy: "\n")
}
}
let line = ""
let line2 = "hello\nworld"
let line3 = "1"
let line4 = "hello\nworld\nthere"
line.lines()
line2.lines()
line3.lines()
line4.lines()