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)  編輯  收藏 所屬分類: 技術文檔
          主站蜘蛛池模板: 襄汾县| 夏河县| 甘泉县| 南漳县| 武平县| 焉耆| 成都市| 腾冲县| 巴楚县| 竹北市| 鄂伦春自治旗| 津南区| 龙江县| 扎鲁特旗| 桓台县| 静安区| 常熟市| 贡山| 庄浪县| 金沙县| 仁化县| 微山县| 松桃| 离岛区| 大渡口区| 靖安县| 曲阳县| 义马市| 乌兰浩特市| 扶风县| 瑞金市| 偏关县| 错那县| 安新县| 永定县| 若尔盖县| 桐城市| 南岸区| 马关县| 辛集市| 时尚|