C++ 分离字符串里的大小写,数字,符号

要求:

1 分离字符串里的大小写,数字,符号

代码如下:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string s("ab2c3d7R4E6");
    string numberics("0123456789");
    string alphabetic("abcdefghijklmnopqrstuvwxyz");
    string alphabetic1("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

   string::size_type pos= 0;
    
   // 查找数字 从s的 0 位置开始查找 npos表示string的末尾
   cout<<"字符串:"<<s<<endl;
   cout<<"-------数字--------"<<endl;
   while((pos=s.find_first_of(numberics,pos))!=string::npos)
   {
        cout<<"s{"<<pos<<"}="<<s[pos++]<<endl;
   }

    pos=0;
    cout<<"---------包含小写----------"<<endl;
   while((pos=s.find_first_of(alphabetic,pos)) != string::npos)
   {
        cout<<"s{"<<pos<<"}="<<s[pos++]<<endl;
   }

    pos=0;
    cout<<"------包含大写--------"<<endl;
   while((pos=s.find_first_of(alphabetic1,pos)) != string::npos)
   {
        cout<<"s{"<<pos<<"}="<<s[pos++]<<endl;
   }

   pos=0;
   cout<<"------不包含数字--------"<<endl;
   while((pos=s.find_first_not_of(numberics,pos)) != string::npos)
   {
        cout<<"s{"<<pos<<"}="<<s[pos++]<<endl;
   }
    system("pause");
    return 0;
}

运行结果:

字符串:ab2c3d7R4E6
-------数字--------
s{3}=2
s{5}=3
s{7}=7
s{9}=4
s{11}=6
---------包含小写----------
s{1}=a
s{2}=b
s{4}=c
s{6}=d
------包含大写--------
s{8}=R
s{10}=E
------不包含数字--------
s{1}=a
s{2}=b
s{4}=c
s{6}=d
s{8}=R
s{10}=E
请按任意键继续. . .




你可能感兴趣的:(C++,字符串分割)