用户可以输入任何字符串字符,唯一允许的特殊字符是 *,它可以放置在字符串的开头或结尾。
例如:
*hello* -> *hello
*h*ello -> *hello
h*llo* -> hello*
hello** -> hello*
**hell**o** -> *hello
我喜欢使用正则表达式来执行此操作。
最佳答案
这个有点用
String s="*he**llo*";//anything
boolean st=(s.startsWith("*"));
boolean et=(s.endsWith("*"));
String ns=s.replaceAll("\\*+","");
ns=(st)?("*"+ns):(et)?(ns+"*"):ns;
关于Java正则表达式仅在开头或结尾保留*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24979066/