Java实现手机号快速格式化
大约 26 分钟
需求
工作需要,经常要对电话号码数组进行各种格式转换和集合运算(交并补),特此编写一个小工具,实现实现手机号快速格式化
号码格式化
格式化X号模式
有这样一个手机号码列表
带前缀的手机号 | 或者真实手机号 |
---|---|
7035519353315435 | 19353315435 |
7035516593621769 | 16593621769 |
7035519279572417 | 19279572417 |
7035519075698040 | 19075698040 |
7035519381122510 | 19381122510 |
7035519531428939 | 19531428939 |
需要转换为逗号表达式手机号列表
19353315435,16593621769,19279572417,...
我们可以在文本编辑器中查找"\r\n"替换为","也可以一键完成
public String getX(String string) {
// 拿到粘贴板内容
String returnString = "";
// 匹配数字 ,空格 说明是有前缀的主叫列表
Pattern pattern3 = Pattern.compile("[0-9\\s]*");
if (pattern3.matcher(string).matches()) {
// 各个系统版本都支持换行分隔符 windows,linux
String[] split = string.split("\\s");
for (String s : split) {
if (s.length() >= 11)
returnString = returnString + s.trim().substring(s.trim().length() - 11) + ',';
}
if (returnString.length() > 1)
return returnString.substring(0, returnString.length() - 1);
}
return returnString;
}
格式化虚拟主叫模式
有这样一个手机号码列表,第一行是前缀
手机号 | - |
---|---|
223 | 这是前缀 |
19353315435 | 手机号 |
16593621769 | 手机号 |
19279572417 | ... |
19075698040 | ... |
19381122510 | ... |
19531428939 | ... |
需要转换为虚拟主叫表达式手机号列表
223:22319353315435;22316593621769;22319279572417......
我们可以在文本编辑器中查找"\r\n"替换为";223",再进行进一步修改...也可以一键完成
public String getVirtually(String string) {
// 拿到粘贴板内容
String returnString = "";
// 匹配数字 ,空格 说明是有前缀的主叫列表
Pattern pattern3 = Pattern.compile("[0-9\\s]*");
String title = "";
if (pattern3.matcher(string).matches()) {
// 各个系统版本都支持换行分隔符
String[] split = string.split("\\s");
title = split[0].trim();
for (String s : split) {
returnString = returnString + title + s.trim() + ';';
}
returnString = title + ":"
+ returnString.substring(title.length() + title.length() + 1, returnString.length() - 1);
}
return returnString.trim();
}
还原格式化主叫
19353315435,16593621769,19279572417,...
逗号表达式执行一次即可还原为号码列表
223:22319353315435;22316593621769;22319279572417......
虚拟主叫模式需要按两次
按下第一次,带前缀 | 第二次11位手机号 |
---|---|
22319353315435 | 19353315435 |
22316593621769 | 16593621769 |
22319279572417 | 19279572417 |
22319075698040 | 19075698040 |
22319381122510 | 19381122510 |
22319531428939 | 19531428939 |
public String getPhone(String string) {
// 拿到粘贴板内容
String returnString = "";
// 匹配数字 : ; , 空格
// Pattern pattern = Pattern.compile("[0-9:;,\\s]*");
// 匹配数字 ,空格 说明是有前缀的主叫列表
Pattern pattern3 = Pattern.compile("[0-9\\s]*");
if (pattern3.matcher(string).matches()) {
// 各个系统版本都支持换行分隔符
String[] split = string.split("\\s");
for (String s : split) {
// 七位前缀
if (s.trim().length() >= 11) {
returnString = returnString + s.trim().substring(s.trim().length() - 11) + System.lineSeparator();
} else {
// 原样输出
// returnString = returnString + s.trim()+ System.lineSeparator();
}
}
return returnString.trim();
}
// 匹配数字 , 空格 说明是X号主叫
Pattern pattern = Pattern.compile("[0-9,\\s]*");
if (pattern.matcher(string).matches()) {
String[] split = string.split(",");
for (String s : split) {
returnString = returnString + s.trim() + System.lineSeparator();
}
// System.err.println(returnString.trim());
return returnString.trim();
}
// 匹配数字 : ; 空格 说明是虚拟主叫
Pattern pattern2 = Pattern.compile("[0-9:;\\s]*");
if (pattern2.matcher(string).matches()) {
String[] split = string.split(";");
for (String s : split) {
returnString = returnString + s.trim() + System.lineSeparator();
}
// System.err.println(returnString.trim());
int index = returnString.indexOf(":");
returnString = returnString.substring(index + 1);
return returnString.trim();
}
return returnString.trim();
}
号码列表的交并补
有这样两个号码列表
列表1 | 列表2 |
---|---|
11111 | 11111 |
22222 | 22222 |
33333 | -- |
44444 | -- |
在生产中经常遇到两个号码列表需要更换配置,需要找出新老号码的相同点和不同点
求交集: |11111|22222|
求并集: |11111|22222|33333|44444|
求补集: |33333|44444|
如果两个号码列表完全相同,那么交集=并集,补集为空
private List<String> in1;
private List<String> in2;
public void phoneMath() {
if (in1 == null || in2 == null) {
return;
}
List<String> strings = new ArrayList<String>(in1);
// 求交集
strings.retainAll(in2);
List<String> strings2 = new ArrayList<String>(in1);
// 求并集
strings2.removeAll(in2);
strings2.addAll(in2);
List<String> strings3 = new ArrayList<String>(strings2);
// 求补集(并集差)
strings3.removeAll(strings); // a c
Paste.setSysClipboardText("交集" + strings.size() + "并集" + strings2.size() + "补集" + strings3.toString());
}
// 读粘贴板
provider.register(KeyStroke.getKeyStroke("control alt C"), new HotKeyListener() {
@Override
public void onHotKey(HotKey arg0) {
String paste = getPaste();
in2 = in1;
String[] split = paste.split("\\s");
List<String> strings = new ArrayList<String>();
for (String s : split) {
if (s.trim().length() > 0)
strings.add(s.trim());
}
in1 = strings;
// System.err.println(in1);
// System.err.println(in2);
}
});
快速转换实现
注册快捷键到全局热键
private void initWindow() {
// 主类是Provider,获取当前平台(操作系统)的提供者
// Provider provider = Provider.getCurrentProvider(useSwingEventQueue);
Provider provider = Provider.getCurrentProvider(true);
// D 还原配置
provider.register(KeyStroke.getKeyStroke("control alt D"), new HotKeyListener() {
@Override
public void onHotKey(HotKey arg0) {
try {
Thread.sleep(200); // 参数为毫秒,所以需要将0.3转换成对应的毫秒值
} catch (InterruptedException e) {
e.printStackTrace();
}
String paste = getPaste();
String phone = getPhone(paste);
setPaste(phone);
}
});
// F 重载为 X号模式
provider.register(KeyStroke.getKeyStroke("control alt F"), new HotKeyListener() {
@Override
public void onHotKey(HotKey arg0) {
try {
Thread.sleep(200); // 参数为毫秒,所以需要将0.3转换成对应的毫秒值
} catch (InterruptedException e) {
e.printStackTrace();
}
String paste = getPaste();
String x = getX(paste);
setPaste(x);
}
});
// V 重载为 虚拟主叫模式
provider.register(KeyStroke.getKeyStroke("control alt V"), new HotKeyListener() {
@Override
public void onHotKey(HotKey arg0) {
try {
Thread.sleep(200); // 参数为毫秒,所以需要将0.3转换成对应的毫秒值
} catch (InterruptedException e) {
e.printStackTrace();
}
String paste = getPaste();
String virtually = getVirtually(paste);
setPaste(virtually);
}
});
// 读粘贴板
provider.register(KeyStroke.getKeyStroke("control alt C"), new HotKeyListener() {
@Override
public void onHotKey(HotKey arg0) {
String paste = getPaste();
in2 = in1;
String[] split = paste.split("\\s");
List<String> strings = new ArrayList<String>();
for (String s : split) {
if (s.trim().length() > 0)
strings.add(s.trim());
}
in1 = strings;
}
});
// 得到交集并集补集
provider.register(KeyStroke.getKeyStroke("control alt X"), new HotKeyListener() {
@Override
public void onHotKey(HotKey arg0) {
System.err.println("control alt X");
try {
Thread.sleep(200); // 参数为毫秒,所以需要将0.3转换成对应的毫秒值
} catch (InterruptedException e) {
e.printStackTrace();
}
phoneMath();
setPaste(Paste.getSysClipboardText().trim());
}
});
}
粘贴板数据读写
//复制
public String getPaste() {
Robot robot;
try {
robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL); // 按下ctrl键盘
robot.keyPress(KeyEvent.VK_C); // 按下ctrl键盘
robot.delay(50);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_C);
robot.delay(100);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Paste.getSysClipboardText().trim();
}
//粘贴
public void setPaste(String s) {
Paste.setSysClipboardText(s);
Robot robot;
try {
robot = new Robot();
robot.delay(50);
robot.keyPress(KeyEvent.VK_CONTROL); // 按下ctrl键盘
robot.keyPress(KeyEvent.VK_V);
robot.delay(50);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
效果
参考
开发-示例集合-Java-系统快捷键
开发-示例集合-Java-系统粘贴板
TODO
使用的awt组件在 linux/mac兼容性不强,仅适合windows使用
在linux中应该使用xsel 命令来实现与剪贴板的交互
性能很弱