gpt4 book ai didi

java - 从字符串中删除字符 '\u202A' 8234

转载 作者:搜寻专家 更新时间:2023-11-01 09:32:00 26 4
gpt4 key购买 nike

我正在尝试获取字符串中索引为 0 的字符:

public static String editNoHP (String noHP){
String result;
try {
if(noHP.charAt(0) == '0')
result = "62"+noHP.substring(1);
else if(noHP.charAt(0) == '+' )
result = noHP.substring(1);
else if(noHP.charAt(0) == '6')
result = noHP;
else if(noHP.charAt(0) == '6' && noHP.charAt(1) == '2')
result = noHP;
else if(noHP.charAt(0) == '9')
result = noHP;
else
result = "62"+noHP;
}
catch (Exception e){
return "";
}

return result.replaceAll("[\\s\\-\\.\\^:,]","");
}

所以我在查询联系人后使用了这个功能,但是我发现了奇怪的结果。

正常输入输出:

input = +62 111-1111-1111   output : 6211111111111
input = 011111111111 output : 6211111111111

这是奇怪的输入和结果:

input = 011111111111        output : 62011111111111

所以我尝试调试这个帐户,我发现当应用程序尝试在 0 处获取字符时,返回的是 '\u202A' 8234,而不是 0。

我已经试过正则表达式了:

String clean = str.replaceAll("[^\\n\\r\\t\\p{Print}]", ""); or
String clean = str.replaceAll("[^\\x20-\\x7E]", ""); or
String clean = str.replaceAll("[^\u0000-\uFFFF]", ""); or
String clean = str.replaceAll("[^\\p{ASCII}]", ""); or
String clean = str.replaceAll("[^\x00-\x7F]", ""); or
String clean = StringEscapeUtils.unescapeJava(str);

它们都返回相同的值 '\u202A' 8234。

这是什么角色?如何解决这个问题?

更新:我尝试编辑奇怪的联系人,但我发现了奇怪的行为。数字是 011111111111。首先我将光标放在 0 和 1 之间,然后我按删除/退格键删除 0。光标突然移动到数字 1 的右侧而不是左侧。然后我保存联系人并运行我的程序。结果是 0,不是 '\u202A' 8234。所以我认为这是因为数字格式不正常,可能是第一次添加此联系人时或从 google 帐户同步时。

最佳答案

最后,我发现我可以使用正则表达式来替换非字母数字字符。

所以这是我的压轴功能:

public static String editNoHP (String noHPinput){
String result;
try {
noHPinput = noHPinput.trim();
String noHP = noHPinput;
noHP = noHP.replaceAll("[\\s\\-\\.\\^:,]","");
noHP = noHP.replaceAll("[^A-Za-z0-9]","");
char isinya = noHP.charAt(0);

if(isinya == '0')
result = "62"+noHP.substring(1);
else if(isinya == '+' )
result = noHP.substring(1);
else
result = noHP;

}
catch (Exception e){
return "";
}

return result;
}

此正则表达式删除字母数字字符旁边的所有 unicode 字符。

关于java - 从字符串中删除字符 '\u202A' 8234,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46362393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com