lmw
2023-06-06 7a563b559c48b9b339784c25fc5f0adc2ab5154e
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package cn.sinata.xldutils.utils
 
import android.net.Uri
import android.util.Base64
import java.io.File
import java.net.URLDecoder
import java.net.URLEncoder
import java.nio.charset.Charset
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.regex.Pattern
 
 
/**
 * 字符串长度。中文算2个字符。英文算1个字符计算。
 */
fun String?.lengthCN(): Int {
 
    if (this.isNullOrEmpty()) {
        return 0
    }
    var len = 0
    var count = 0
    while (count < this!!.length) {
        val c = get(count)
        len += if (c.toInt() < 128) {
            1
        } else {
            2
        }
        count++
    }
    return len
}
 
/**
 * 是否null或者'null'字符
 */
fun String?.isNulls(): Boolean = isNullOrEmpty() || equals("null")
 
/**
 * urlEncode
 */
fun String?.urlEncode(): String {
    return if (isNullOrEmpty()) "" else URLEncoder.encode(this, "utf-8")
}
 
/**
 * urlDecode
 */
fun String?.urlDecode(): String {
    return if (isNullOrEmpty()) "" else URLDecoder.decode(this, "utf-8")
}
 
/**
 * base64
 */
fun String?.base64(charset: Charset = Charsets.UTF_8): String {
    return if (isNullOrEmpty()) "" else Base64.encodeToString(this!!.toByteArray(charset), Base64.DEFAULT)
}
 
/**
 * 是否有效手机号
 */
fun String?.isValidPhone(): Boolean {
    if (isNullOrEmpty()) {
        return false
    }
    if (this!!.length != 11) {
        return false
    }
    val expression = "(^(13|14|15|16|17|18|19)[0-9]{9}$)"
    val pattern = Pattern.compile(expression)
    val matcher = pattern.matcher(this)
    if (matcher.matches()) {
        return true
    }
    return false
}
/**
 * 是否有效坐机号
 */
fun String?.isValidPhone2(): Boolean {
    if (isNullOrEmpty()) {
        return false
    }
    val expression = "^0(10|2[0-5789]|\\d{3})\\d{7,8}$"
    val pattern = Pattern.compile(expression)
    val matcher = pattern.matcher(this)
    if (matcher.matches()) {
        return true
    }
    return false
}
 
/**
 * 是否有效邮箱地址
 */
fun String?.isValidEmail(): Boolean {
    if (isNullOrEmpty()) {
        return false
    }
    val pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")
    val matcher = pattern.matcher(this)
    if (matcher.matches()) {
        return true
    }
    return false
}
 
/**
 * 是否有效密码(包含大小字母、数字、特称字符,至少8个字符,最多30个字符)
 */
fun String?.isValidPassword(): Boolean {
    if (isNullOrEmpty()) {
        return false
    }
    val pattern = Pattern.compile("(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,30}")
    val matcher = pattern.matcher(this)
    if (matcher.matches()) {
        return true
    }
    return false
}
 
/**
 * 返回隐藏后的手机号,
 */
fun String?.hidePhone(): String {
    if (isNullOrEmpty()) {
        return ""
    }
    if (this!!.length < 7) {
        return this
    }
    return substring(0, 3) + "****" + substring(length - 4, length)
}
 
/**
 * 隐藏身份证号,必须不为null并且length大于10才返回处理后的字符串
 */
fun String?.hideIdCard(): String {
    if (isNullOrEmpty()) {
        return ""
    }
    if (this!!.length < 10) {
        return this
    }
    return substring(0, 6) + "*****" + substring(length - 4, length)
}
 
fun String?.isFilePath(): Boolean {
    if (isNullOrEmpty()) {
        return false
    }
    return File(this).exists()
}
 
/**
 * md5字符串
 */
fun String?.md5(): String {
    if (this == null) {
        return ""
    }
    try {
        val bmd5 = MessageDigest.getInstance("MD5")
        bmd5.update(this.toByteArray())
        var i: Int
        val buf = StringBuffer()
        val b = bmd5.digest()
        for (offset in b.indices) {
            i = b[offset].toInt()
            if (i < 0) {
                i += 256
            }
            if (i < 16) {
                buf.append("0")
            }
            buf.append(Integer.toHexString(i))
        }
        return buf.toString()
    } catch (e: NoSuchAlgorithmException) {
        e.printStackTrace()
    } catch (e: NullPointerException) {
        e.printStackTrace()
    }
    return ""
}
 
fun String?.toImageUri(): Uri {
    if (this == null) {
        return Uri.parse("")
    }
    return Uri.parse(this)
}
 
fun String?.isLetterDigit(): Boolean {
    if (this == null) {
        return false
    }
    var isDigit = false//定义一个boolean值,用来表示是否包含数字
    var isLetter = false//定义一个boolean值,用来表示是否包含字母
    this.forEach {
        if (Character.isDigit(it)) {
            isDigit = true
        }
        //用char包装类中的判断字母的方法判断每一个字符
        if (Character.isLetter(it)) {
            isLetter = true
        }
    }
    val regex = "^[a-zA-Z0-9]+$"
    return isDigit && isLetter && matches(Regex(regex))
}
/**
 * 是否有效身份证
 */
fun String?.isValidIdCard(): Boolean {
    if (isNullOrEmpty()) {
        return false
    }
    if (this!!.length != 18) {
        return false
    }
    val expression = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]\$)"
    val pattern = Pattern.compile(expression)
    val matcher = pattern.matcher(this)
    if (matcher.matches()) {
        return true
    }
    return false
}
 
fun String.isVideo():Boolean{
    if (isNullOrEmpty()) {
        return false
    }
    return (endsWith("flv")||endsWith("mp4")||endsWith("mkv")||endsWith("avi"))
}