http://archive.devx.com/free/tips/tipview.asp?content_id=4151

Who knew in this day and age flipping bits to change case is still publishable (this 
is from today!)

Barry Caplan
www.i18n.com
Vendor Showcase: http://Showcase.i18n.com


--------------------------------------------------------------

Use Logical Bit Operations to Changing Character Case


This is a simple example demonstrating my own personal method.

// to lower case
  public char lower(int c)
  {
       return (char)((c >= 65 && c <= 90) ? c |= 0x20 : c);
  }

//to upper case
  public char upper(int c)
  {
    return (char)((c >= 97 && c <=122) ?  c ^= 0x20 : c);
  }
/*
 If I would I could create a method for converting an entire
string to lower, like this:
*/
  public String getLowerString(String s)
  {
     char[] c = s.toCharArray();
     char[] cres = new char[s.length()];
     for(int i=0;i<c.length;++i)
         cres[i] = lower(c[i]);
     return String.valueOf(cres);
  }
/*
even converting in capital:
*/
  public String capital(String s)
  {
     return
String.valueOf(upper(s.toCharArray()[0])).concat(s.substring(1));
  }
/* using it....*/
public static void main(String args[])
  {
     x xx = new x();
     System.out.println(xx.getLowerString("LOWER: " + "FRAME"));
     System.out.println(xx.upper('f'));
     System.out.println(xx.capital("randomaccessfile"));
}


Reply via email to