查找目标字符串中的子串,可识别大小写,大写和小写认为相同

来源:互联网 发布:网络传真机怎么安装 编辑:程序博客网 时间:2024/05/03 16:45

// ComParaString.cpp : Defines the entry point for the console application.
//

#pragma  warning (disable : 4786)
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool non_case(char c1,char c2)
{
 return toupper(c1) == toupper(c2);
}

/********************************************************************
 created: 2010/01/22
 created: 22:1:2010   11:42
 filename:  D:/work/20100115/ComParaString/ComParaString.cpp
 file path: D:/work/20100115/ComParaString
 file base: ComParaString
 file ext: cpp
 author:  Victor
 
 purpose: Srcstr 目标字符串。DstString 源字符串,substring 返回的字符串, 可识别大小写字母,认为相同,如果返回值为false substring为,空

*********************************************************************/
bool SubString(string Srcstr,string DstString,string &substring)
{

 bool  flag =  false;
 
 
 if (Srcstr.size()==DstString.size()&&equal(Srcstr.begin(),Srcstr.end(),DstString.begin(),non_case))
 {
  flag = true;
  
 }
 
 for (int i = DstString.size();i>0;i--)
  
 {
  DstString.resize(i);
  string::iterator strop;
  strop = search(Srcstr.begin(),Srcstr.end(),DstString.begin(),DstString.end(),non_case);
  if (strop==Srcstr.end())
  {
   flag = false;
  }
  else
  {
   if (!flag)
   {
    size_t index  = strop-Srcstr.begin();
    substring.append(Srcstr,index,DstString.size()); 
   }
   
   flag = true;
   
  }
 }
 return flag;
}
int main(int argc, char* argv[])
{
 char *s = "adddasdfsdfas";
 char *s1 = "asdfa";
 string strdst;
 SubString(s,s1,strdst);
 cout<<strdst<<endl;

 return 0;
}

原创粉丝点击