Playing with HTMLTopSome useful String methodsConverting strings

Converting strings

   public String toLowerCase()
   public String toUpperCase()

Creates a new version of a string that is either all lower case or all upper case. Does not change the original string.

Demo 2. A case-insensitive word counter/finder differs from the earlier version only by the added two lines in the following.

  private int substringCounter( String text, String word)
  {
        int count = 0;
        int pos;
        
        text = text.toLowerCase();
        word = word.toLowerCase();
        
        pos = text.indexOf(word,0);
        
        while ( pos >= 0 )
        {
            count++;
            pos = text.indexOf(word,pos+word.length());
        
        }
        return count;
  }

Note that the assignment statements to text and word are required. String methods do not manipulate the given string. They make a brand new one.


Playing with HTMLTopSome useful String methodsConverting strings