程式狂想筆記

一個攻城師奮鬥史

0%

中文姓名遮罩和判斷字串有中英文方法

網路收集幾個方法

判斷中英文方法

判断字符串是否纯中文、英文、数字、等等_心丨悦 的专栏-CSDN博客

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void isEnglish(String str) {
//【全为英文】返回true 否则false
boolean result1 = str.matches("[a-zA-Z]+");
//【全为数字】返回true
boolean result6 = str.matches("[0-9]+");
//【除英文和数字外无其他字符(只有英文数字的字符串)】返回true 否则false
boolean result2 = str.matches("[a-zA-Z0-9]+");
//【含有英文】true
String regex1 = ".*[a-zA-z].*";
boolean result3 = str.matches(regex1);
//【含有数字】true
String regex2 = ".*[0-9].*";
boolean result4 = str.matches(regex2);
//判断是否为纯中文,不是返回false
String regex3 = "[\\u4e00-\\u9fa5]+";
boolean result5 = str.matches(regex3);
System.out.println(result1 + "--" + result2 + "--" + result3
+ "--" + result4 + "--" + result5 + "--" + result6);
}

中英文姓名判斷

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
public class HelloWorld{
public static void main(String[] args) {

String id = "A123456789";

System.out.println(maskName("黃藥師")); // A1*****789
System.out.println(maskName("Hello World")); // A1234*****
System.out.println(maskName("Huang d")); // ####456789

}


public static String maskName(String name){

String result = "";
if (isEnglish(name)) {
result = mask(name, 2, name.length()-3, '*');
}else{
result = mask(name, 1, name.length()-2, '*');
}
return result;
}

//判断是不是英文字母
public static boolean isEnglish(String charaString) {
if( charaString == null && "".equals(charaString)){
return false;
}
return charaString.substring(0,1).matches("^[a-zA-Z]*");
}
/**
* 字串遮罩
* @param text 原始字串
* @param start 遮罩起始位置index
* @param length 遮罩長度
* @param maskSymbol 遮罩符號
* @return 遮罩過的字串
*/
private static String mask(String text, int start, int length, char maskSymbol) {
if (text == null || text.isEmpty()) {
return "";
}
if (start < 0) {
start = 0;
}
if (length < 1) {
return text;
}

StringBuilder sb = new StringBuilder();
char[] cc = text.toCharArray();
for (int i = 0; i < cc.length; i++) {
if (i >= start && i < (start + length)) {
sb.append(maskSymbol);
} else {
sb.append(cc[i]);
}
}
return sb.toString();
}
}

遮罩長度可以改

1
2
3
4
5
6
7
8
9
10
public static String maskName(String name){

String result = "";
if (isEnglish(name)) {
result = mask(name, 2, name.length()-3 > 8 ? 8 : name.length()-3 , '*');
}else{
result = mask(name, 1, name.length()-2 > 8 ? 8 : name.length()-2 , 'o');
}
return result;
}

sql 判斷方法

oracle 判断字符是否为字母,oracle字符_数据库前沿资讯 | 和通数据库
How to mask characters with X in a varchar2 field in Oracle SQL - Stack Overflow

1
2
3
4
5
6
7
8
--- postgressql
select substr(a.name,1,2)|| repeat('*',length(a.name)-2)|| substr(a.name,length(a.name)-1,2),a.name,ascii(substring(a.name,1,1)) between 65 and 122,
CASE WHEN ascii(substring(a.name,1,1)) between 65 and 122 then
substr(a.name,1,2)|| repeat('*',length(a.name)-3)|| substr(a.name,-1,1)
else
substr(a.name,1,1)|| repeat('*',length(a.name)-2)|| substr(a.name,-1,1)
end as mask_name,length(a.name)
from account a
1
2
3
4
5
6
7
8
9

--oracle
select substr(full_name,1,5) || regexp_replace(substr(full_name,6,5), '[^ ]', 'X') ||
substr(full_name,11,5) || regexp_replace(substr(full_name,16,5), '[^ ]', 'X') ||
substr(full_name,21,5) || regexp_replace(substr(full_name,26,5), '[^ ]', 'X') as masked_name
from
(
select 'Tristram Vladimir Chan ' as full_name from dual
)