天天看點

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

請按任意鍵繼續. . .

繼續閱讀