0

I may just be dead wrong, but I've used String.split(String) for a very long time and I've never ran into an issue with this, basically I'm trying to split a string by a period, and return it into an array called breadcrumbs

Current code:

String[] breadcrumbs = file.toString()
                .replaceAll(".java", "")
                .replaceAll("\\\\", ".")
                .split(".");
System.out.println("Length: " + breadcrumbs.length);

The length returns zero,

Now, if I take the .split(".") off and make breadcrumbs a regular string, like so:

String breadcrumbs = file.toString()
                .replaceAll(".java", "")
                .replaceAll("\\\\", ".");
System.out.println(breadcrumbs);

It prints out a perfectly good directory, using periods (Like I want), here's the output:

C:.Users.Jellal.Desktop.Java Workspace.ogserver-framework.src.test.com.chris.server.user.Account

So, as you can see we have a perfectly good string to work with, taking it into account that this worked, and the "Single-line" code I posted first didn't, I tried it while it was seperated, like so:

String fileDir = file.toString()
                .replaceAll(".java", "")
                .replaceAll("\\\\", ".");
String[] breadcrumbs = fileDir.split(".");
System.out.println(fileDir);
System.out.println(breadcrumbs.length);

This generates the following output:

C:.Users.Jellal.Desktop.Java Workspace.ogserver-framework.target.test-classes.com.chris.server.user.Account.class
0

Not exactly sure what's going on here, but it seems broken.

Hobbyist
  • 15,888
  • 9
  • 46
  • 98
  • 1
    `String#Split()` takes regex. in regex dot (`.`) has special meaning, so you need to escape it with double backslash – DnR Nov 28 '14 at 04:14
  • @DnR - Thanks, I wasn't aware that the '.' had a special meaning in regex and it was throwing me for a loop. I've never really messed with the regex. – Hobbyist Nov 28 '14 at 04:18

1 Answers1

2

The parameter of split is a regular expression. You need to do split("\\.").

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-

The111
  • 5,757
  • 4
  • 39
  • 55
  • While this works, even after reading the link you posted (Again) it still doesn't make sense to me, I've used String.split(String) for years without issue, what's with the strange regex requirements all of a sudden? Is it just because of the character that I'm using? `.`? – Hobbyist Nov 28 '14 at 04:16
  • 3
    There are no "sudden" requirements, it's been that way since (at least) Java 5. Whether or not the requirements are "strange" is an opinion, but "what's with" them is that that is the way the API was designed, likely for flexibility. ;-) Somehow you've gone years without noticing it, and that most likely is indeed because until now you haven't used a special char like `.` as you say. It's an easy mistake to make, I know I've made it before. – The111 Nov 28 '14 at 04:26
  • It is because of the character you are using . which is a metacharacter thus it must be written as either\\. or \\Q.\\E or [.] . Though the third of these options does not appear to be documented, it does work. https://docs.oracle.com/javase/tutorial/essential/regex/literals.html – R Schultz Nov 28 '14 at 04:40