Java字符代码

时间:2014-11-11 12:36:37

标签: java string character-codes

我正在使用indexOf在字符串中查找终结符字符。

// character codes to look for
int[] defseps = new int[] {10, 13, 34, 36, 38, 39,41, 44, 59, 63, 93, 125};
int t = 999999999;  // big number,  We want first terminator 
int tempt = 0;

// loop through possible terminators
for (int x = 0; x < 12; x++) {
    tempt=str.indexOf(defseps[x]); // Get index of terminator
    if (defsepactivated[x] && tempt!=-1) {  // If active terminator found
        System.out.println("defsep used=" + defseps[x]);
        if (tempt < t) t = tempt; // Use this terminator if before previous found  
    }
}

此代码查找终结符,如&amp; (38)和](93)但不是双引号(34)。

例如,如果str是: = THINGTHUNG&#34 ;; copyright&amp; copy ,它会找到半冒号和&amp;但不是双引号。

非常感谢为什么在我尝试其他编码之前会出现这种情况。

1 个答案:

答案 0 :(得分:0)

假设您要查找任何一个&#34;终结符&#34;的第一个出现的索引。字符串中的字符,使用字符的字符类正则表达式:

if (!str.matches(".*[\n\r\"#&'),;?\\]}].*")) {
    // handle not found
} else {
    int t = str.split("[\n\r\"#&'),;?\\]}]")[0].length - 1;
}

仅供参考,大多数角色在角色课程中失去了特殊的正则表达式,并且不需要转义,但当然](关闭角色类)必须被转义。