-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtils.kt
119 lines (101 loc) · 3.75 KB
/
StringUtils.kt
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package tech.appzilla.com.setmydate.Utils
class StringUtils private constructor() {
init {
throw Error("U will not able to instantiate it")
}
companion object {
/**
* Determines whether the string is null or a length of 0
* *
* * @param s String to be verified
* * @return `true`: Empty `false`: Not empty
*/
private fun isEmpty(s: CharSequence?): Boolean {
return s == null || s.length == 0
}
/**
* Determine whether the string is null or all spaces
* *
* * @param s String to be verified
* * @return `true`: null or full space <br></br> `false`: not null and not all spaces
*/
fun isBlank(s: String?): Boolean {
return s == null || s.trim { it <= ' ' }.isEmpty()
}
/**
* Determine whether two strings are equal
* *
* * @param a to be verified string a
* * @param b String to be verified b
* * @return `true`: Equal to `false`: Not equal
*/
// /**
// * Check whether the two strings are case-insensitive
// *
// * @param a to be verified string a
// * @param b String to be verified b
// * @return {@code true}: Equal to {@code false}: Not equal
// */
// public static boolean equalsIgnoreCase(String a, String b) {
// return (a == b) || (b != null) && (a.length() == b.length()) && a.regionMatches(true, 0, b, 0, b.length());
// }
/**
* Null to a string of length 0
* *
* * @param s to be transferred to the string
* * @return s is null to a string of length 0, otherwise it will not change
*/
fun null2Length0(s: String?): String {
return s ?: ""
}
/**
* Returns the length of the string
* *
* * @param s String
* * @return null Returns 0, others returns its own length
*/
private fun length(s: CharSequence?): Int {
return s?.length ?: 0
}
/**
* Initial capitalization
* *
* * @param s to be transferred to the string
* * @return The first letter of the uppercase string
*/
fun upperFirstLetter(s: String): String? {
return if (isEmpty(s) || !Character.isLowerCase(s[0])) s else (s[0].toInt() - 32).toChar().toString() + s.substring(
1
)
}
/**
* First letter lowercase
* *
* * @param s to be transferred to the string
* * @return The first letter of the lowercase string
*/
fun lowerFirstLetter(s: String): String? {
return if (isEmpty(s) || !Character.isUpperCase(s[0])) {
s
} else (s[0].toInt() + 32).toChar().toString() + s.substring(1)
}
/**
* *
* * @param s To reverse the string
* * @return Reverse the string
*/
fun reverse(s: String): String {
val len = length(s)
if (len <= 1) return s
val mid = len shr 1
val chars = s.toCharArray()
var c: Char
for (i in 0 until mid) {
c = chars[i]
chars[i] = chars[len - i - 1]
chars[len - i - 1] = c
}
return String(chars)
}
}
}