Reputation: 723
as input my program gets a String containing IP Addresses are separated by a line delimiter, i.e. one IP Address per line. To validate each of the addresses I do:
String[] temp;
temp = address.split(System.getProperty("line.separator"));
and then I loop though the array of Strings.
I was wondering why all but the last IP Address were always invalid. I've found out, that they look like 10.1.1.1^M
Is there a way to tell the java.lang.String.split to drop the delimiter before putting the token into the array? Or what other options do I have here? Sorry, I'm not a Java Ninja, so I thought I'll ask you guys before I start googling for hours.
Thanks Thomas
Upvotes: 2
Views: 491
Reputation: 533500
It appears you are using a different carriage return from that of the platform. (e.g. editing on MS-DOS/Windows and running on Linux)
I would use \\s+
to break on any number of white spaces. This will also trim leading or trailing spaces.
Upvotes: 1
Reputation: 62583
Why don't you just use address.split("\\s+")
since valid IP addresses can never contain spaces in them?
Predefined character classes
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
Upvotes: 5
Reputation: 1325
Your line.separator is not valid. It depends on the system you are using:
\n = CR (Carriage Return) // Used as a new line character in Unix
\r = LF (Line Feed) // Used as a new line character in Mac OS
\n\r = CR + LF // Used as a new line character in Windows
Upvotes: 0
Reputation: 838156
The problem is that the delimiter in your file is "\r\n"
, but the value of System.getProperty("line.separator")
is "\n"
. This means that the "\r"
is not treated as part of the delimiter.
Upvotes: 6