I am trying to split String with dot i am not able to get answer
String dob = "05.08.2010";
String arr[] = dob.split(".");
System.out.println(arr[0]+":"+arr[1]+":"+arr[2]);
String.split
takes a regular expression pattern. .
matches any character in a regex. So you're basically saying "split this string, taking any character as a separator". You want:
String arr[] = dob.split("\\.");
... which is effectively a regex pattern of \.
, where the backslash is escaping the dot. The backslash needs to be doubled in the string literal to escape the backslash as far as the Java compiler is concerned.
Alternatively, you could use
String arr[] = dob.split(Pattern.quote("."));
... or much better use date parsing/formatting code (e.g. SimpleDateFormat
or DateTimeFormatter
) to parse and format dates. That's what it's there for, after all - and it would be better to find data issues (e.g. "99.99.9999") early rather than late.
See more on this question at Stackoverflow