从字符串中删除所有分数符号,如“¼”和“½” [英] Removing all fraction symbols like “¼” and “½” from a string

查看:244
本文介绍了从字符串中删除所有分数符号,如“¼”和“½”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将类似于¼杯糖的字符串修改为杯糖,这意味着用替换所有分数符号。

I need to modify strings similar to "¼ cups of sugar" to "cups of sugar", meaning replacing all fraction symbols with "".

我推荐了到这个发布和设法使用以下行删除¼:

I have referred to this post and managed to remove ¼ using this line:

itemName = itemName.replaceAll("\u00BC", "");

但如何更换每个可能的分数符号?

but how do I replace every possible fraction symbol out there?

推荐答案

分数符号,如 ¼ ½ 属于Unicode类别 数字,其他[否] 。如果您可以删除该组中的所有676个字符,则可以使用以下正则表达式:

Fraction symbols like ¼ and ½ belong to Unicode Category Number, Other [No]. If you are ok with eliminating all 676 characters in that group, you can use the following regular expression:

itemName = itemName.replaceAll("\\p{No}+", "");

如果没有,您可以随时明确列出:

If not, you can always list them explicitly:

// As characters (requires UTF-8 source file encoding)
itemName = itemName.replaceAll("[¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞↉]+", "");

// As ranges using unicode escapes
itemName = itemName.replaceAll("[\u00BC-\u00BE\u2150-\u215E\u2189]+", "");

这篇关于从字符串中删除所有分数符号,如“¼”和“½”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆