|
| 1 | +package com.taonce.utilmodule |
| 2 | + |
| 3 | +import java.lang.StringBuilder |
| 4 | +import java.security.MessageDigest |
| 5 | +import java.security.NoSuchAlgorithmException |
| 6 | +import java.util.* |
| 7 | +import java.util.regex.Pattern |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Author: taoyongxiang |
| 12 | + * Date: 2019/4/1 |
| 13 | + * Desc: 字符串相关工具 |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * 判断字符串是否都是数字 |
| 18 | + */ |
| 19 | +fun String.isNumAll(): Boolean { |
| 20 | + val pattern: Pattern = Pattern.compile("[0-9]*") |
| 21 | + return pattern.matcher(this).matches() |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * 验证字符串是否符合手机号规则 |
| 26 | + */ |
| 27 | +fun String.isPhoneNum(): Boolean { |
| 28 | + val rule = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\\d{8}$" |
| 29 | + return (!this.isEmpty() && this.matches(Regex(rule))) |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * 判断字符串是否都是字母 |
| 34 | + */ |
| 35 | +fun String.isLetterAll(): Boolean { |
| 36 | + val pattern: Pattern = Pattern.compile("[a-z]*") |
| 37 | + return pattern.matcher(this).matches() |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * 判断车牌号是否规则 |
| 42 | + */ |
| 43 | +fun String.isCarNum(): Boolean = |
| 44 | + (!this.isEmpty() |
| 45 | + && this.matches(Regex("^[\u4e00-\u9fa5]{1}[A-H_J-N_P-Z]{1}[A-H_J-N_P-Z_0-9]{5,6}$"))) |
| 46 | + |
| 47 | +/** |
| 48 | + * 判断身份证号码是否规则 |
| 49 | + */ |
| 50 | +fun String.isIdCard(): Boolean { |
| 51 | + val pattern: Pattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])") |
| 52 | + return pattern.matcher(this).matches() |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * 指定范围内的大写转小写 |
| 57 | + * [startIndex] 开始的下标 |
| 58 | + * [endIndex] 结束的下标,此下标对应的字母不转 |
| 59 | + * @return ABCD [3,4] -> ABCd |
| 60 | + */ |
| 61 | +fun String.toLowerCase(startIndex: Int = 0, endIndex: Int = this.length): String { |
| 62 | + val subString: String = this.substring(startIndex, endIndex) |
| 63 | + return if (0 <= startIndex && startIndex <= this.length |
| 64 | + && 0 <= endIndex && endIndex <= this.length |
| 65 | + && startIndex <= endIndex |
| 66 | + && !this.isEmpty() |
| 67 | + ) this.replaceFirst( |
| 68 | + subString, |
| 69 | + subString.toLowerCase(Locale.getDefault()) |
| 70 | + ) else "" |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * 指定范围内的小写转大写 |
| 75 | + * [startIndex] 开始的下标 |
| 76 | + * [endIndex] 结束的下标,此下标对应的字母不转 |
| 77 | + * @return abcd [3,4] -> abcD |
| 78 | + */ |
| 79 | +fun String.toUpperCase(startIndex: Int = 0, endIndex: Int = this.length): String { |
| 80 | + val subString: String = this.substring(startIndex, endIndex) |
| 81 | + return if (0 <= startIndex && startIndex <= this.length |
| 82 | + && 0 <= endIndex && endIndex <= this.length |
| 83 | + && startIndex <= endIndex |
| 84 | + && !this.isEmpty() |
| 85 | + ) this.replaceFirst( |
| 86 | + subString, |
| 87 | + subString.toLowerCase(Locale.getDefault()) |
| 88 | + ) |
| 89 | + else "" |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * MD5加密 |
| 94 | + */ |
| 95 | +fun String.md5(): String { |
| 96 | + return if (this.isEmpty()) "" |
| 97 | + else { |
| 98 | + try { |
| 99 | + val md5: MessageDigest = MessageDigest.getInstance("MD5") |
| 100 | + val bytes: ByteArray = md5.digest(this.toByteArray()) |
| 101 | + return with(StringBuilder()) { |
| 102 | + bytes.forEach { |
| 103 | + val temp: String = Integer.toHexString(it.toInt() and 0xff) |
| 104 | + if (temp.length == 1) { |
| 105 | + append("0").append(temp) |
| 106 | + } else { |
| 107 | + append(temp) |
| 108 | + } |
| 109 | + } |
| 110 | + toString() |
| 111 | + } |
| 112 | + } catch (e: NoSuchAlgorithmException) { |
| 113 | + "" |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * SHA-1加密 |
| 120 | + */ |
| 121 | +fun String.sha1(): String { |
| 122 | + return if (this.isEmpty()) "" |
| 123 | + else { |
| 124 | + try { |
| 125 | + val md5: MessageDigest = MessageDigest.getInstance("SHA-1") |
| 126 | + val bytes: ByteArray = md5.digest(this.toByteArray()) |
| 127 | + return with(StringBuilder()) { |
| 128 | + bytes.forEach { |
| 129 | + val temp: String = Integer.toHexString(it.toInt() and 0xff) |
| 130 | + if (temp.length == 1) { |
| 131 | + append("0").append(temp) |
| 132 | + } else { |
| 133 | + append(temp) |
| 134 | + } |
| 135 | + } |
| 136 | + toString() |
| 137 | + } |
| 138 | + } catch (e: NoSuchAlgorithmException) { |
| 139 | + "" |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * SHA-256加密 |
| 146 | + */ |
| 147 | +fun String.sha256(): String { |
| 148 | + return if (this.isEmpty()) "" |
| 149 | + else { |
| 150 | + try { |
| 151 | + val md5: MessageDigest = MessageDigest.getInstance("SHA-256") |
| 152 | + val bytes: ByteArray = md5.digest(this.toByteArray()) |
| 153 | + return with(StringBuilder()) { |
| 154 | + bytes.forEach { |
| 155 | + val temp: String = Integer.toHexString(it.toInt() and 0xff) |
| 156 | + if (temp.length == 1) { |
| 157 | + append("0").append(temp) |
| 158 | + } else { |
| 159 | + append(temp) |
| 160 | + } |
| 161 | + } |
| 162 | + toString() |
| 163 | + } |
| 164 | + } catch (e: NoSuchAlgorithmException) { |
| 165 | + "" |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments