String类中无法识别特殊字符

时间:2017-02-07 19:06:44

标签: java string special-characters

这是java.String.indexOf(char)

的错误解决方案

我一直在研究逻辑计算器的算法,并且 String 类没有检测到一些普遍接受的逻辑字符(即未被检测到String.indexOf("⇔");被召唤了)

我能够创建一个解决方案,我在这里发布它以帮助其他有类似问题的人。

/* 
 * Compares the decimal value of each char to the decimal value of the char
 * that isn't detected by java.String 
 */

   String string = "any char with ⇔ decimal value";         
   int[] charAsDecimal = new int[string.length() -1];
   int locationOfSpeicalChar = 0;

   for(int x = 0; x < string.length(); x++)
   {
       charAsDecimal[x] = (int)string.charAt(x);
   }
   for(int x = 0; x < string.length(); x++)
   {
       if(charAsDecimal[x] == 8660)
       {
           System.out.println("⇔ is at index value " + x);
           locationOfSpeicalChar = x;
       }
   }

   System.out.println(string);
   string = string.substring(locationOfSpeicalChar);
   System/out.println(string);

   /*
    * ⇔ in decimal is 8660 and can be used to find in charAsDecimal. The index value
    * in charAsDecimal is the same index value as in string.
    */

1 个答案:

答案 0 :(得分:1)

首先,没有必要写string.length()-1,这种方法你丢失了一个字符

第二,我不知道这段代码中的E是什么,这里没有定义,可能你需要将char数组转换为int数组,试试看这样:

String string = "any char with decimal value";       
int[]  a = new int[string.length()];

for(int x = 0; x < string.length() ; x++){
    a[x] = (int)string.charAt(x);
    System.out.println("value at " + x + " : " + string.charAt(x) + " in dec = " + a[x]);
}