leochiang

          BlogJava 聯系 聚合 管理
            21 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks
          首先,判斷一個字符串中是否含有雙字節字符:
           1 String str = "test中文漢字";
           2 String regEx = "[//u4e00-//u9fa5]";
           3 
           4 /**
           5 * 判斷有沒有中文
           6 
          */
           7 if (str.getBytes().length == str.length()) {
           8     System.out.println("無漢字");
           9 } else {
          10     System.out.println("有漢字");
          11 }
          12 
          13 /**
          14 * 如果有則打印出來
          15 
          */
          16 Pattern p = Pattern.compile(regEx);
          17 Matcher m = p.matcher(str);
          18 while (m.find()) { 
          19     System.out.print(m.group(0) + "");
          20 }                                                                                                                                                                                                                                                                                                                        


          其次,是關于昨天的面試題街區字符串的問題:
          package core_java;

          public class StringInter {
              public static boolean vd(char c){
                        
                  boolean isGB2312=false
                  byte[] bytes=(""+c).getBytes(); 
                  if(bytes.length==2){ 
                              int[] ints=new int[2]; 
                              ints[0]=bytes[0]& 0xff; 
                              ints[1]=bytes[1]& 0xff; 
                              if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){ 
                                  isGB2312=true
                              } 
                  } 
                   
                  return isGB2312; 
              }
              static String Interception(String ss, int nn) {
                  if( nn > (ss.length())) nn = (ss.length());
                  for(int i=0; i <= ss.length(); i++){
                      //System.out.print("now nn is " +nn + "\n");
                      if( nn <= 0 ) return ss;
                      char t = ss.charAt(i);
                      if(vd(t)) {    //說明是漢字 nn減二
                          if(nn == 1) return ss;
                          System.out.print(t);
                          nn-=2;
                      }
                      else {                // 非漢字,nn減一
                          System.out.print(t);
                          nn--;
                      }
                  }
                  return ss;
              }
              public static void main( String args[] ) {
                  String a = "aま哈abcdefg"; 
                  // length() =    charAt(0..)  
                  
          //asd我是System.out.print(a.length());
                  
          //Scanner in = new Scanner(System.in);
                  
          //String s = in.nextLine();
                  
          //int n = in.nextInt();
                  Interception(a, 2);
              }
          }





          posted on 2012-07-31 16:10 leoChiang 閱讀(627) 評論(0)  編輯  收藏 所屬分類: 技術文檔
          主站蜘蛛池模板: 津市市| 从化市| 柳州市| 体育| 类乌齐县| 漳平市| 丹棱县| 若羌县| 浑源县| 库伦旗| 扬中市| 萝北县| 望奎县| 永清县| 基隆市| 海伦市| 三都| 阳东县| 大渡口区| 沈丘县| 五常市| 横山县| 龙门县| 辽阳县| 洛南县| 余江县| 武威市| 宾阳县| 乌鲁木齐县| 个旧市| 柘荣县| 呈贡县| 尼勒克县| 德州市| 荔浦县| 思南县| 汉中市| 阜康市| 新营市| 斗六市| 清镇市|