Case Insensitive Array Search
-
My question is simple, I want to search a string in an array and it must not be case sensitive.
private static String arraySearch (String[] myArray)
{Arrays.sort(myArray,String.CASE\_INSENSITIVE\_ORDER); String name=""; int index = Arrays.binarySearch(myArray, "java"); name = "Found Java at: " + index; return name; }
Above is just a procedure, the array list is stated in main method. How can I search a word that ignore case sensitive whether its lower or upper case?
-
My question is simple, I want to search a string in an array and it must not be case sensitive.
private static String arraySearch (String[] myArray)
{Arrays.sort(myArray,String.CASE\_INSENSITIVE\_ORDER); String name=""; int index = Arrays.binarySearch(myArray, "java"); name = "Found Java at: " + index; return name; }
Above is just a procedure, the array list is stated in main method. How can I search a word that ignore case sensitive whether its lower or upper case?
private int arraySearch(String[] haystack, String needle)
{
//index -1 incase it is not found.
int index = -1;//To stop the loop is the String is found. boolean run\_i = true; //Go through the entire array. for (int i = 0; i < haystack.length && run\_i; i++) { //If both lowercase match if(haystack\[i\].trim().toLowerCase().equals(needle.trim().toLowerCase())) { //Set the index of which the String was found. index = i; //String found so stop loop. run\_i = false; } } //Return the index of the return index;
}
ps. I wrote this on the fly so it may have errors but it is the general idea.
hmmm pie