C++ 判断 IP 是否在 1网段。 char string 查找 截取 比较

需求 判断 IP 是否在 1网段,思路是判断字符串里是否包含192.168.1 ,找了3个方法:

char *strstr(const char *str1, const char *str2);

此函数的功能为查询字符串 str2 是否是 str1 的子串,如果是,返回字符串str2在str1中出现的头一个位置的指针*char。如果不是,则返回null;

substr(size_type __pos = 0, size_type __n = npos) const

获得字符串s中 从第0位开始的长度为n 的字符串 ,第一个参数是起始位置,第二个参数是切割长度

char * __cdecl strcpy(char * __restrict__ _Dest,const char * __restrict__ _Source);

将一个字符串的内容复制到另一个字符串中

int __cdecl strcmp(const char *_Str1,const char *_Str2);

把 str1 所指向的字符串和 str2 所指向的字符串进行比较(1、若参数1>参数2,返回正数;2、若参数1<参数2,返回负数;3、若参数1=参数2,返回0;)

std::vector<std::string> ipls=  whudp.getIpList(); // ip地址集合
for(int i=0;i<ipls.size();i++){
    // 1
    const char * ip= ipls[i].c_str();
    // char str2[] = "192.168.1.";
    // char * ss = strstr(ip, str2);
    char * ss = strstr(ip, "192.168.1.");
    if(ss == nullptr) {// 地址
        cout << "no-====  " << ip << endl;
        continue;
    }

    // 2
    string ss = ipls[i].substr(0, 10);
    if(ss == "192.168.1.") {
         cout << "yes-====  " << ss << endl;
    }

    // 3
    char t[100] = {0};
    char str2[] = "192.168.1.";
    const  char * ip= ipls[i].c_str();
    strcpy(t, ip);
    if (strcmp(t, str2) == 0){
        cout << "yes-====  " << ip << endl;
    } else {
        cout << "no====  " << ip << endl;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值