java – 用除了URL之外的字符串中的波斯数字替换英文数字
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 用除了URL之外的字符串中的波斯数字替换英文数字,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1936字,纯文字阅读大概需要3分钟。
内容图文
![java – 用除了URL之外的字符串中的波斯数字替换英文数字](/upload/InfoBanner/zyjiaocheng/805/8074f18fb9544852995a0f0ba4ec3572.jpg)
我为波斯语字符串编写了一个自定义TextView,这个TextView应该将英文数字替换成波斯数字
public class PersianTextView extends TextView {
public PersianTextView(Context context) {
super(context);
}
public PersianTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PersianTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setText(CharSequence text, BufferType type) {
String t = text.toString();
t = t.replaceAll("0", "?");
t = t.replaceAll("1", "?");
t = t.replaceAll("2", "?");
t = t.replaceAll("3", "?");
t = t.replaceAll("4", "?");
t = t.replaceAll("5", "?");
t = t.replaceAll("6", "?");
t = t.replaceAll("7", "?");
t = t.replaceAll("8", "?");
t = t.replaceAll("9", "?");
super.setText((CharSequence)t, type);
}
}
此View工作正常,但如果我从HTML设置文本,包含英文数字的链接将不会显示为链接!
什么是解决问题最简单的方法?
解决方法:
某种形式需要在链接上进行模式匹配.
private static final Pattern DIGIT_OR_LINK_PATTERN =
Pattern.compile("(\\d|https?:[\\w_/+%?=&.]+)");
// Pattern: (dig|link )
private static final Map<String, String> PERSIAN_DIGITS = new HashMap<>();
static {
PERSIAN_DIGITS.put("0", "?");
PERSIAN_DIGITS.put("1", "?");
PERSIAN_DIGITS.put("2", "?");
PERSIAN_DIGITS.put("3", "?");
PERSIAN_DIGITS.put("4", "?");
PERSIAN_DIGITS.put("5", "?");
PERSIAN_DIGITS.put("6", "?");
PERSIAN_DIGITS.put("7", "?");
PERSIAN_DIGITS.put("8", "?");
PERSIAN_DIGITS.put("9", "?");
}
public static String persianDigits(String s) {
StringBuffer sb = new StringBuffer();
Matcher m = DIGIT_OR_LINK_PATTERN.matcher(s);
while (m.find()) {
String t = m.group(1);
if (t.length() == 1) {
// Digit.
t = PERSIAN_DIGITS.get(t);
}
m.appendReplacement(sb, t);
}
m.appendTail(sb);
return sb.toString();
}
附:
根据文本的不同,最好只替换HTML标记之外的数字,即>之间的数字.和<.
private static final Pattern DIGIT_OR_LINK_PATTERN =
Pattern.compile("(\\d|<[^>]*>)",
Pattern.DOTALL|Pattern.MULTILINE);
内容总结
以上是互联网集市为您收集整理的java – 用除了URL之外的字符串中的波斯数字替换英文数字全部内容,希望文章能够帮你解决java – 用除了URL之外的字符串中的波斯数字替换英文数字所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。