Separate IP parts
biozic via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Dec 10 00:03:00 PST 2016
On Saturday, 10 December 2016 at 03:51:34 UTC, brocolis wrote:
> How do I separate IP parts with dlang?
>
> I found this very cool trick, with C++:
> http://stackoverflow.com/a/5328190
>
> std::string ip ="192.168.1.54";
> std::stringstream s(ip);
> int a,b,c,d; //to store the 4 ints
> char ch; //to temporarily store the '.'
> s >> a >> ch >> b >> ch >> c >> ch >> d;
> std::cout << a << " " << b << " " << c << " "<< d;
>
> I wonder what's the equivalent D code.
This would do the same. I wouldn't say it's a trick.
import std.format : formattedRead;
import std.stdio : writefln;
string ipAddr = "192.168.1.54";
int a, b, c, d;
formattedRead(ipAddr, "%d.%d.%d.%d", &a, &b, &c, &d);
writefln("%s %s %s %s", a, b, c, d);
More information about the Digitalmars-d-learn
mailing list