本文介绍了如何在一个字符串计数特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
Using replaceAll:
String str = "one$two$three$four!five@six$";
int count = str.length() - str.replaceAll("\\$","").length();
System.out.println("Done:"+ count);
Prints:
Done:4
Using replace instead of replaceAll would be less resource intensive. I just showed it to you with replaceAll because it can search for regex patterns, and that's what I use it for the most.
Note: using replaceAll I need to escape $, but with replace there is no such need:
str.replace("$");
str.replaceAll("\\$");
这篇关于如何在一个字符串计数特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!