簡體   English   中英

Java字符代碼

[英]Java 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  
    }
}

此代碼查找類似於&(38)和](93)的終止符,但找不到雙引號(34)。

例如,如果str是: = THINGTHUNG“; copyright&copy,它將找到分號和&,但不能找到雙引號。

非常感謝為什么在嘗試其他編碼之前會是這種情況。

假設您要查找字符串中任何“終止符”字符首次出現的索引,請使用字符的字符類正則表達式:

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

僅供參考,大多數字符在字符類中時都不需要特殊的正則表達式,並且不需要轉義,但是] (關閉字符類)當然必須轉義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM