• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養德,非澹薄無以明志,非寧靜無以致遠。
            隨筆 - 397, 文章 - 0, 評論 - 196, 引用 - 0
            數據加載中……

            根據特定分割符拆分字符串、判斷字符串是否為IP

            ////////////////////////////////////////////////////////////
            //函數名: splitEx
            //功能:    根據特定的分隔符拆分字符串
            //輸入參數:string& src-------------需要拆分的原字符串
            //          char ch-----------------分隔符
            //返回值:  string類型的動態數組(vector)
            /////////////////////////////////////////////////////////////  
            vector<string> Zone::splitEx(string& src, char ch)  
            {  
              vector<string> strs; 
             string last(&ch);
             string pro_str;
             char c_temp;

              //去掉字符串中的雙引號
             std::string::size_type First_pos = src.find("\"");
             std::string::size_type Last_pos = src.find_last_of("\"");
             if (First_pos == 0  && Last_pos !=std::string::npos)
             {
              src[Last_pos]='\0';
              string t_sTemp;
              for (int i=0;i<(int)src.size();++i)
              {
               if(i>0)
               {
                t_sTemp += src[i];
               }
              }
              strs.insert(strs.end(),t_sTemp);
              return strs;
             }

              //將字符串中多個連續的分隔符壓縮成一個
             for (int i=0;i<(int)src.size();++i)
             {
              if(i>0)
              {
                  if (src[i] == ch && src[i-1]==ch)
                  {
                     continue;
                  }
               }
                c_temp=src[i];
                pro_str += c_temp;
             }
               
             src=pro_str;

             int separate_characterLen = 1;//分割字符串的長度
                int lastPosition = 0,index = -1;  
                while (-1 != (index = src.find(ch,lastPosition)))  
                {  
                   string Temp= src.substr(lastPosition,index - lastPosition);
                   if(!Temp.empty())
                   {
                        strs.push_back(Temp));  
                    }
                    lastPosition = index + separate_characterLen;  

                }  
                string lastString = src.substr(lastPosition);//截取最后一個分隔符后的內容   
                if (!lastString.empty() && lastString != last)
                {
                  //cout<<"push_back_end"<<endl;  
                   strs.push_back(lastString);//如果最后一個分隔符后還有內容就入隊 
                 }

                return strs;  
            }  
            ////////////////////////////////////////////////////////////
            //函數名: IsIp
            //功能:    判斷字符串是否為IP
            //輸入參數:string In-------字符串
            //返回值:  true-------是IP,false----------不是IP
            //修改記錄:暫無
            /////////////////////////////////////////////////////////////
            bool Zone::IsIp(string In)
            {
              int ip[4]={-1,-1,-1,-1};
              sscanf(In.c_str(),"%d.%d.%d.%d",&(ip[0]),&(ip[1]),&(ip[2]),&(ip[3]));
              if (ip[0]>-1 && ip[1]>-1 && ip[2]>-1 && ip[3]>-1)//是IP
              {
                   return true;
              }
              else
              {
                  return false;
              }
            }

            判斷是否為IP,也可以用inet_pton函數
             unsigned char buf[sizeof(struct in6_addr)];  
                int domain, s;  
                char str[INET6_ADDRSTRLEN];  
             
                //IP字符串 ——》網絡字節流  
                s = inet_pton(AF_INET, In.c_str(), buf);  
                if(s<=0)
                {  
                    return false;
                }
               
                return true;
            In是string對象,如果是IPV6,將 inet_pton的第一個參數改成AF_INET6,但是這個函數只在
            linux下,windows下是沒有的。在liunx下要包含sys/types.h、sys/socket.h、arpa/inet.h
            原型:int inet_pton(int af, const char *src, void *dst); 這個函數轉換字符串到網絡地址,第一個參數af是地址族,轉換后存在dst中
            返回值如果小于等于0就表示ip不合法。
            在windows平臺下,在windows的SDKs\v6.0A\Include\ws2tcpip.h, inet_pton was
            defined when NTDDI_VERSION >= NTDDI_LONGHORN with the following lines:

            #if (NTDDI_VERSION >= NTDDI_LONGHORN)
            WINSOCK_API_LINKAGE
            INT
            WSAAPI
            inet_pton(
                __in                                INT             Family,
                __in                                PCSTR           pszAddrString,
                __out_bcount(sizeof(IN6_ADDR))      PVOID           pAddrBuf
                );
            但是要注意windows版本(NTDDI_LONGHORN的windows版本是Windows Server 2008)

            posted on 2011-04-26 20:38 Benjamin 閱讀(1702) 評論(0)  編輯 收藏 引用 所屬分類: C/C++

            久久国产精品99久久久久久老狼 | 久久99久久成人免费播放| 色欲久久久天天天综合网精品| 精品永久久福利一区二区| 精品免费tv久久久久久久| 中文字幕一区二区三区久久网站 | 久久综合亚洲色HEZYO社区| 久久久高清免费视频| 日产精品久久久一区二区| 久久这里只有精品首页| 亚洲国产成人精品无码久久久久久综合 | 青青青伊人色综合久久| 久久亚洲2019中文字幕| 奇米综合四色77777久久| 国产精品免费久久久久影院| 久久精品国产精品亚洲精品 | 精品久久久噜噜噜久久久| 一本色道久久88加勒比—综合| 大香伊人久久精品一区二区| 精品久久久无码人妻中文字幕豆芽 | 久久精品三级视频| 久久婷婷五月综合97色| 久久综合五月丁香久久激情| av无码久久久久不卡免费网站 | 色婷婷噜噜久久国产精品12p| 久久99国内精品自在现线| 国产精品乱码久久久久久软件 | 久久婷婷国产麻豆91天堂| 久久国语露脸国产精品电影| 久久精品一区二区影院| 久久免费视频观看| 久久精品国内一区二区三区| 少妇内射兰兰久久| 99久久国产综合精品女同图片| 久久久噜噜噜久久中文字幕色伊伊| 99精品久久久久中文字幕| 日日噜噜夜夜狠狠久久丁香五月| 久久夜色精品国产噜噜亚洲a| 亚洲乱码日产精品a级毛片久久| 99久久成人18免费网站| 国产高清国内精品福利99久久|