////////////////////////////////////////////////////////////
//函數名: 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)