0
String realstring = "&&&.&&&&";
Double value = 555.55555;
String[] arraystring = realstring.split(".");
String stringvalue = String.valueof(value);
String [] valuearrayed = stringvalue.split(".");
System.out.println(arraystring[0]);

Sorry if it looks bad. Rewrote on my phone. I keep getting ArrayIndexOutOfBoundsException: 0 at the System.out.println. I have looked and can't figure it out. Thanks for the help.

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
Lifetake
  • 191
  • 4
  • 11
  • 8
    split takes a regexp as argument, not a literal string. You have to escape the dot. Or you could also simply use indexOf('.') and substring(). – JB Nizet Feb 14 '14 at 21:51
  • 1
    @JBNizet I feel like that should be the answer :) – C.B. Feb 14 '14 at 21:52
  • 1
    I succumbed to the pressure :) – JB Nizet Feb 14 '14 at 21:55
  • @JBNizet can you explain more – Lifetake Feb 14 '14 at 21:56
  • 1
    @T.J.Crowder Are you sure? I get `java.lang.ArrayIndexOutOfBoundsException: 0` from that link. – Paul Bellora Feb 14 '14 at 21:57
  • 1
    @PaulBellora: And now, so do I. (Comment deleted, but here's the link: http://ideone.com/mCBYAV) How weird. Well, IDEOne's UI is so disjointed, I probably misread it. (Still a very handy tool.) Weird that it returns an empty array, but I just read your now-deleted comment ("Splitting on `.` means splitting on any character, and by default `split` leaves off trailing empty strings, and splitting on any character gives you *only* empty strings" or something like that) on JB's answer, and that explains it nicely! – T.J. Crowder Feb 14 '14 at 22:05

3 Answers3

8

split() takes a regexp as argument, not a literal string. You have to escape the dot:

string.split("\\.");

or

string.split(Pattern.quote("."));

Or you could also simply use indexOf('.') and substring() to get the two parts of your string.

And if the goal is to get the integer part of a double, you could also simply use

long truncated = (long) doubleValue;
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
7

split uses regex as parameter and in regex . means "any character except line separators", so you could expect that "a.bc".split(".") would create array of empty strings like ["","","","",""]. Only reason it is not happening is because (from split javadoc)

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

so because all strings are empty you get empty array (and that is because you see ArrayIndexOutOfBoundsException).

To turn off removal mechanism you would have to use split(regex, limit) version with negative limit.

To split on . literal you need to escape it with \. (which in Java needs to be written as "\\." because \ is also Strings metacharacter) or [.] or other regex mechanism.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
2

Dot (.) is a special character so you need to escape it.

String realstring = "&&&.&&&&";         
String[] partsOfString = realstring.split("\\.");
String part1 = partsOfString[0];
String part2 = partsOfString[1];
System.out.println(part1);

this will print expected result of

&&&

Its also handy to test if given string contains this character. You can do this by doing :

if (string.contains(".")) {
    // Split it.
} else {
    throw new IllegalArgumentException("String " + string + " does not contain .");
}
Maciej Cygan
  • 5,351
  • 5
  • 38
  • 72