For example, I have this string: 10.10.10.10/16
and I want to remove the mask from that IP and get: 10.10.10.10
How could this be done?
Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.
Use sscanf() like this: char subStr[3]; sscanf("M1[r2][r3]", " %2[^[]", subStr); where [^[] means a character except [ and 2 guards the length of the string written into the substring (it's equal to subStr 's size - 1, so that space for the NULL terminator is available). as BLUEPIXY suggested.
std::string x = "dog:cat"; std::string substr; auto npos = x. find(":"); if (npos != std::string::npos) substr = x.
Here is how you would do it in C++ (the question was tagged as C++ when I answered):
#include <string>
#include <iostream>
std::string process(std::string const& s)
{
std::string::size_type pos = s.find('/');
if (pos != std::string::npos)
{
return s.substr(0, pos);
}
else
{
return s;
}
}
int main(){
std::string s = process("10.10.10.10/16");
std::cout << s;
}
Just put a 0 at the place of the slash
#include <string.h> /* for strchr() */
char address[] = "10.10.10.10/10";
char *p = strchr(address, '/');
if (!p)
{
/* deal with error: / not present" */
;
}
else
{
*p = 0;
}
I don't know if this works in C++
char* pos = strstr(IP,"/"); //IP: the original string
char [16]newIP;
memcpy(newIP,IP,pos-IP); //not guarenteed to be safe, check value of pos first
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With