查找文本长度减去转义字符



如何在不计算某些转义符的情况下找到字符串的长度?我正在寻找一种方法,而不是自己一次检查每个字符。。。除了.length()之外还有什么函数可用?我试图输出条目的字符数,但当我键入return时,它会添加一个字符

使用string.replacement或.replaceall将转义符替换为空字符串",然后使用长度?

String replace(CharSequence target, CharSequence replacement)复制此字符串,用另一个序列替换指定目标序列。

String replace(char oldChar, char newChar)复制此字符串,用另一个字符替换指定字符。

String replaceAll(String regularExpression, String replacement)用给定的替换项替换此字符串中regularExpression的所有匹配项。

取自http://developer.android.com/reference/java/lang/String.html

int count1 = StringUtils.countMatches("your_string", "your escape char1");
int count2 = StringUtils.countMatches("your_string", "your escape char2");
int count3 = StringUtils.countMatches("your_string", "your escape char3");
int count4 = StringUtils.countMatches("your_string", "your escape char4");
...
result = your_string_lenght - (count1 + count2 + count3 + count4);

它并不完全是java或android。我认为伪是清楚的。

最新更新