Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have a String something like this

"myValue"."Folder"."FolderCentury";

I want to split from dot("."). I was trying with the below code:

String a = column.replace("\"", "");
String columnArray[] = a.split(".");

But columnArray is coming empty. What I am doing wrong here?

I will want to add one more thing here someone its possible String array object will contain spitted value like mentioned below only two object rather than three.?

columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
share|improve this question
I have added some more specification in question so it should not be look like duplicate. – Programmer Aug 29 at 10:26

marked as duplicate by Uwe Plonus, allprog, Jakob S, Hong Ooi, Mark Hurd Aug 29 at 15:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 7 down vote accepted

Note that String#split takes a regex.

You need to escape the special char . (That means "Any charachter"):

 String columnArray[] = a.split("\\.");

(Escaping a regex is done by \, but in Java, \ is written as \\).

You can also use Pattern#quote:

Returns a literal pattern String for the specified String.

String columnArray[] = a.split(Pattern.quote("."));

By escaping the regex, you tell the compiler to treat the . as the String . and not the special char ..

share|improve this answer
i have added one more thing in same question is this possible? – Programmer Aug 29 at 10:25
columnArray[0]= "myValue"."Folder"; This won't compile. – Maroun Maroun Aug 29 at 10:54
Yes but String is inside columnArray[0] and it would worked i have some more complexity how these value coming only i want how i can split it like two string – Programmer Aug 29 at 11:02
I don't understand your question. Can you please elaborate? – Maroun Maroun Aug 29 at 11:07
I have asked here see if this can exaplin you my problem stackoverflow.com/questions/18508782/… – Programmer Aug 29 at 11:08

You must escape the dot.

String columnArray[] = a.split("\\.");
share|improve this answer

split() accepts an regular expression. So you need to skip '.' to not consider it as a regex meta character.

String[] columnArray = a.split("\\."); 
share|improve this answer

The next code:

   String input = "myValue.Folder.FolderCentury";
   String regex = "(?!(.+\\.))\\.";
   String[] result=input.split(regex);
   System.out.println(Arrays.toString(result));

Produces the required output:

[myValue.Folder, FolderCentury]

The regular Expression tweaks a little with negative look-ahead (this (?!) part), so it will only match the last dot on a String with more than one dot.

share|improve this answer

While using special characters need to use the particular escape sequence with it.

'.' is a special character so need to use escape sequence before '.' like:

 String columnArray[] = a.split("\\.");
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.