Linux 处理中文字符串 :/区分中文和英文的方法
最近工作中遇到一个问题:要在一大堆 中文字符串中 分割 : 中文冒号:由于中文是unicode 所以用不了 strtok 函数。自己分割的第一步就是要自己找到固定中文字符的位置:
#include <stdio.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <locale.h> #include <wchar.h> int chinese_or_english(char *str,int len) { // char *p=NULL; char chinese[4] = {0}; for (int i = 0; i < len; i++) { if ((str[i] & 0x80) == 0) { //chinese:the top is 1 // printf("alpha:%c\n", str[i]); } else { chinese[0] = str[i]; chinese[1] = str[i + 1]; chinese[2] = str[i + 2]; i++; i++; printf("chinese:%s i is %d\n", chinese,i); if(strcmp(chinese,":")==0 ) { //p=str+i; printf("find >>>>>>>>>>>>>> chinese:%s i is %d\n", chinese,i); return i; } } } } int main() { char buff_s[128]="我算出来了,等于:二"; printf("buff_s len is [%d]\n",strlen(buff_s)); //char p=buff_s; int i =0; char want_buffer[64]={"\0"}; int n=0; char *r=NULL; n=chinese_or_english(buff_s,strlen(buff_s)); int j=0; printf("n is %d\n",n); if(n !=0) { //r=chinese_or_english+n; for(i=n+1;i<strlen(buff_s);i++) { want_buffer[j]=buff_s[i]; j++; printf("i is %d\n",i); } } //want_buffer[i]='\0'; printf("want_buffer is %s\n",want_buffer); /* for(i=0;i<30;i++) { if(i%3==0 && i >0) { printf("%c%c%c\n",buff_s[i-1],buff_s[i],buff_s[i+1]); printf("\n"); } printf("%x ",buff_s[i]); } */ /* while(p != ":") { printf("*p is %c\n",*p); p++; } printf("p====:,ps is %c%c\n",*p,*(p+1)); char*temp = strtok(buff_s,":"); while(temp) { printf("%s \n",temp); temp = strtok(NULL,":"); } */ return 0; }