您现在的位置:阿里巴巴博客 > 依星工作室 > 文章
我的文章

VC中实现类似VB的函数Left,Right,Mid,Len等 (2008/06/23 09:08)

VC中实现类似VB的函数Left,Right,Mid,Len(GetCharNum);具体实现代码如下(本代码在WINDOWS XP +VC++6.0下通过):

//取得字符左边字符(实现VB中的Left)
CString Left(CString strInfo, int iNum)
{
 CString infotmp=_T("-1");
 if(iNum<=0 || strInfo.IsEmpty())
  return infotmp;
 int cnums=0;//字符数
 int lnums=0;//字符长度
 int a=0;

 while(strInfo.GetLength()>lnums)
 {
     char c=strInfo.GetAt(lnums);
     if (c<0 || c>255) //是中文
     {
      a=2;
     }
     else
     {
      a=1;
     }
     cnums++;
     lnums+=a;

     if (cnums==iNum)
     {
      break;
     }

 }
 infotmp=strInfo.Left(lnums);

 return infotmp;
}
//取得字符右边字符(实现VB中的Right)
CString Right(CString strInfo, int iNum)
{
 CString infotmp=_T("-1");
 if(iNum<=0 || strInfo.IsEmpty())
  return infotmp;
 int cnums=0;//字符数
 int lnums=strInfo.GetLength();//字符长度
 int a=0;
 int inums=0;

 while(lnums>=0)
 {
     char c=strInfo.GetAt(lnums-1);
     if (c<0 || c>255) //是中文
     {
      a=2;
     }
     else
     {
      a=1;
     }
     cnums++;
     lnums-=a;
     inums+=a;

     if (cnums==iNum)
     {
      break;
     }

 }
 infotmp=strInfo.Right(inums);

 return infotmp;
}
//实现VB中的Mid(不区分英文字符及汉字UNICODE)
CString Mid(CString strInfo, int iPos, int iNum)
{
 CString sValue=_T("-1");
 //判断是否为错
 if(iPos<=0 || iNum<=0 || strInfo.IsEmpty())
  return sValue;
 //
 CString sValuetmp;
 sValuetmp=Right(strInfo,GetCharNum(strInfo)-iPos+1);//
 sValue=Left(sValuetmp,iNum);

 return sValue;
}
//获得字符个数(实现VB中的Len)
int GetCharNum(CString strInfo)
{
 CString infotmp;
 int cnums=0;
 int enums=0;
 int nums,a;
 infotmp=strInfo;
 while(infotmp.GetLength()>0)
 {
    if(infotmp!=_T("") || infotmp!=NULL)
    {
     char c=infotmp.GetAt(infotmp.GetLength()-1);
     if (c<0 || c>255) //是中文
     {
      cnums++;
      a=2;
     }
     else
     {
      enums++;
      a=1;
     }
     infotmp=infotmp.Left(infotmp.GetLength()-a);
    }else
     break;
 }
 nums=cnums+enums;
 return nums;
}


时间仓促,仅实现功能,未加任何容错处理;不足之处,欢迎相互交流。
email:airen3339@126.com
QQ:34596561 312337667
MSN:airen3339@hotmail.com
2008-6-23(依星)



红色精灵的相关文章 >>更多
红色精灵的相关标签
文章评论以下评论只代表个人观点,不代表阿里巴巴网商博客的观点或立场
暂时无网友评论
欢迎您加入阿里巴巴商人博客!在这里您将会结交更多商友,分享更多经验!得到更多推荐与展示的机会!博客宝贝先教您几招——《扩大知名度,巧用博客三板斧!》详情查看:http://blog.china.alibaba.com/html/static1/subject/first.html
发表评论请严格遵守相关法律,严禁恶意评论和垃圾评论

登录名:    密 码:    没有帐号?马上注册
 
内 容:
验证码:
点击图片,换一张  看不清,请点击换图
无显示 无显示
博主信息
山不在高,有仙则名。水不在深,有龙则灵。斯是陋室,唯吾德馨。
加为好友
打个招呼
定制此作者文章
我的商铺
  • 访问量:6443
  • 文章数:13
  • 评论数:18
  • 创建日期:2005-11-19 14:31:27
RSS订阅
个人资料
最新博文
最新评论
最近访客
我的文章分类
好友列表
最近更新博客
友情链接
编辑推荐