Search code examples
javastringspecial-characters

Special char not recognized in String class


This is a solution to an error with java.String.indexOf(char)

I have been working on algorithms for a logic calculator and some of the commonly accept logic characters weren't being detected by the String class (ie was not detected when String.indexOf("⇔"); was called)

I was able to create a solution and I am posting it here to help others with similar problems.

/* 
 * 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.
    */

Solution

  • first , there is no need to write string.length()-1 , with this approach you are losing one character

    second , i didn't know what is E in this code , it is not defined here , probably you need to convert the char array to int array , try this way :

    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]);
    }