Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Case Insensitive Array Search

Case Insensitive Array Search

Scheduled Pinned Locked Moved Java
questionjavadatabasedata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LordLothar
    wrote on last edited by
    #1

    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?

    F 1 Reply Last reply
    0
    • L LordLothar

      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?

      F Offline
      F Offline
      fly904
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups