欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 大小寫最快轉(zhuǎn)換方式實(shí)例代碼

 更新時(shí)間:2017年07月14日 10:06:44   作者:hpgary  
這篇文章主要介紹了Java 大小寫最快轉(zhuǎn)換方式實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

Java 大小寫最快轉(zhuǎn)換方式實(shí)例代碼

         這里直接給出實(shí)現(xiàn)代碼,在代碼中注釋都很清楚,不多做介紹。

Java代碼 

package io.mycat; 
 
import java.util.stream.IntStream; 
/** 
 * 小寫字母的 'a'=97 大寫字母 A=65 更好相差32利用這個(gè)差進(jìn)行大小寫轉(zhuǎn)換 
 * @author : Hpgary 
 * @date : 2017年5月3日 10:26:26 
 * @mail: hpgary@qq.com 
 * */ 
public class StringUtils { 
 
  protected final static byte[] CHAR_TYPE = new byte[512]; 
 
  protected final static byte CHARACTER_DIFFER = 32; 
 
  static { 
    /** 
     * 先將大寫字母放入 CHAR_TYPE 中,將大寫轉(zhuǎn)換成為小寫字母 
     * */ 
    IntStream.rangeClosed('A', 'Z').forEach(c -> CHAR_TYPE[c] = (byte) (c + CHARACTER_DIFFER)); 
    /** 
     * 將小寫字母放入 CHAR_TYPE,存值為小寫字母 
     * */ 
    IntStream.rangeClosed('a', 'z').forEach(c -> CHAR_TYPE[c] = (byte) (c)); 
  } 
   
  public static byte[] toUpperCase(String src) { 
    byte[] bytes = src.getBytes(); 
    for (int x = 0; x < bytes.length; x++) { 
      int tmpLen = bytes[x] << 1; 
      if (tmpLen < CHAR_TYPE.length && tmpLen >= 0) { 
        byte b = CHAR_TYPE[bytes[x]]; 
        if (b != 0) { 
          bytes[x] = (byte) (b - CHARACTER_DIFFER); 
        } 
      } 
    } 
    return bytes; 
  } 
 
  public static byte[] toLowerCase(String src) { 
    byte[] bytes = src.getBytes(); 
    for (int x = 0; x < bytes.length; x++) { 
      int tmpLen = bytes[x] << 1; 
      if (tmpLen < CHAR_TYPE.length && tmpLen >= 0) { 
        byte b = CHAR_TYPE[bytes[x]]; 
        if (b != 0) { 
          bytes[x] = b; 
        } 
      } 
    } 
    return bytes; 
  } 
 
  public static void main(String[] args) { 
    int count = 100000 ;  
    String str = "fdajfadSKfj1221SDKfdasfdsafjdsafjlsadjfkl;sdajflksadjlfkjasdlk;fjasdklfasdA" ; 
     
    long time2 = System.currentTimeMillis(); 
    for (int x = 0; x < count; x++) { 
      str.toUpperCase(); 
    } 
    System.out.println(System.currentTimeMillis() - time2); //51 - 53 
     
    long time1 = System.currentTimeMillis(); 
    for (int x = 0; x < count; x++) { 
      toUpperCase(str) ;  
    } 
    System.out.println(System.currentTimeMillis() - time1); // 35-37 
  } 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!如有疑問請(qǐng)留言,或者到本站社區(qū)討論!

相關(guān)文章

最新評(píng)論