zoukankan      html  css  js  c++  java
  • c++ linux 判断string是中文的 or 英文的 字符串。

    #include <iostream>  
    #include <string.h>  
    #include <stdio.h>  
    #include <stdlib.h>  
    using namespace std;  
    /***linux下一个中文占用三个字节,windows占两个字节***/  
    void chinese_or_english(char *str)  
    {  
      char chinese[4] = {0};  
      for (int i = 0; i < strlen(str); i++) {  
        //if (str[i] >= 0 && str[i] <= 127) {      //ascII  
        if ((str[i] & 0x80) == 0) {   //chinese:the top is 1  
          cout<<"alpha:"<<str[i]<<endl;  
        }  
        //else if (str[i] < 0){  
        else {  
          chinese[0] = str[i];  
          chinese[1] = str[i + 1];  
          chinese[2] = str[i + 2];  
          i++;    //skip one more  
          i++;  
          printf("chinese:%s
    ", chinese);  
        }  
      }  
    }  
    int main()  
    {  
      char str[] = "tai太阳yang";  
      cout<<strlen(str)<<endl;  
      chinese_or_english(str);  
      return 0;  
    }

    判断占用字节数。

    int SVPSettingKeyboard::chinese_or_english(std::string str,int index)  
    {
        if(index > str.size()-1)
            return 0;
        if((str[index-1]& 0x80) == 0){
            return 1;
        }else{
            int n = 2;
            while(((str[index-n] &0x80) == 0x80 )&&((str[index-n] &0x40) == 0)){
                ++n;
            }
            return n;
        }
    }
  • 相关阅读:
    Java程序员从笨鸟到菜鸟全部博客目录
    Problem I
    Problem I
    Problem S
    Problem S
    Problem X
    Problem X
    Problem Q
    Problem Q
    Ellipse
  • 原文地址:https://www.cnblogs.com/yuguangyuan/p/8809137.html
Copyright © 2011-2022 走看看