WassiM ZgheiB
WassiM ZgheiB

Reputation: 119

Manipulating IP addresses - split string on '.' character

String address = "192.168.1.1";

I want to split the address and the delimiter is the point. So I used this code:

String [] split = address.split(".");

But it didn't work, when I used this code it works:

String [] split = address.split("\\.");

so why splitting the dot in IPv4 address is done like this : ("\\.") ?

Upvotes: 4

Views: 15802

Answers (2)

sol4me
sol4me

Reputation: 15708

You should split like this, small tip use Pattern.compile as well

String address = "192.168.1.1";
String[] split = address.split("\\.");// you can replace it with private static final Pattern.

Upvotes: 2

Boris the Spider
Boris the Spider

Reputation: 61178

You need to escape the "." as split takes a regex. But you also need to escape the escape as "\." won't work in a java String:

String [] split = address.split("\\.");

This is because the backslash in a java String denotes the beginning of a character literal.

Upvotes: 10

Related Questions