public static boolean isLetter(char c){
int k=0X80;
return c/k==0?true:false;
}
public static int lengths(String strSrc){
if (strSrc==null){
return 0;
}
int len=0;
char[] strChar=strSrc.toCharArray();
for (int i=0;i
if (!isLetter(strChar[i])) len++;
}
return len;
}
public static String subString(String origin,int len){
if (origin==null || origin.equals("")|| len<1){
return "";
}
if (len>lengths(origin)){
return origin;
}
byte[] strByte=new byte[len];
System.arraycopy(origin.getBytes(),0,strByte,0,len);
int count=0;
for (int i=0;i
if (value<0) count++;
}
if (count % 2 !=0){
len=(len==1)?++len:--len;
}
return new String(strByte,0,len);
}
public static void main(String[] args) {
System.out.println(""+ subString("我ABC汉DEF",6));
}
void get_string(char *s,int n)
{
int i,t=0;
for (i=0;i
if(s[n]&0x80) t++;
}
if(t%2)n--;
s[n] = 0;
printf("%s",s);
}
#include
#include
#include
char *GetSubString(char *res, int size)
{
int i, j;
int len = strlen(res);
// char *des = new char[len+1];
char *des = (char*) malloc(len+1);
/* if (size > len)
{
size = len;
}
else if (size<0)
{
size = 0;
} */
for (i=0, j=0; i
/*汉字是使用两个字节来存放的,如果高位字节的最高位为1那么就是汉字了*/
/*也可以用 res[i]<0 作为条件*/
if( (~(res[i]>>8)) == 0 )
{
size+=1;
des[j] = res[i];
j++;
i++;
}
des[j] = res[i];
}
des[j]=0;
return des;
}
void main()
{
char str[256] = "彼bi岸an花开huakai";
char *p = GetSubString(str, 6);
printf("%s\n", p);
// delete[] p;
free(p);
}
谢谢,学习中