-1

I have a string like this:

String str="\"myValue\".\"Folder\".\"FolderCentury\"";

Is it possible to split the above string by . but instead of getting three resulting strings only two like:

columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";

Or do I have to use an other java method to get it done?

Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
Programmer
  • 455
  • 2
  • 8
  • 25
  • 1
    can u generalize this more? If it is need based then the below answer works.. but if you can give some more generalized scenario that will help. – dharam Aug 29 '13 at 11:05
  • 1
    What you have above is not a valid String. In Java you cannot use quotes inside a string without escaping them. For the same reason, the first of your assignments is invalid as well. Please update your question and fix these errors to help us better understand your problem. – Dušan Rychnovský Aug 29 '13 at 11:05
  • @Dusan this is coming of JSON Object and everything look same as i defined in question. – Programmer Aug 29 '13 at 11:07
  • 1
    What if there are more then two dots inside the string? Such as "\"first\".\"second\".\"third\".\"fourth\"". – Dušan Rychnovský Aug 29 '13 at 11:11
  • 1
    If this is coming from some JSON, I would recommend using a JSON parser to read it, rather than trying to do it yourself. – AJMansfield Aug 29 '13 at 11:14
  • @dusan it should not in my case – Programmer Aug 29 '13 at 11:18

3 Answers3

4

Try this.

     String s = "myValue.Folder.FolderCentury";
     String[] a = s.split(java.util.regex.Pattern.quote("."));

Hi programmer/Yannish,

First of all the split(".") will not work and this will not return any result. I think java String split method not work for . delimiter, so please try java.util.regex.Pattern.quote(".") instead of split(".")

Ashok kumar
  • 435
  • 4
  • 13
  • 1
    For the downvoter: You _do_ need the `java.util.regex.Pattern.quote(".")` for this to be correct (or you need to manually enter your string as `"\\."`). `java.lang.String.split()` takes as its parameter a _regular expression_, not just a specific string to split on. Without this modification, it will split the string on _every character_, resulting in an array of `s.length()+1` empty strings as the result. – AJMansfield Aug 29 '13 at 11:11
3

If the problem you're trying to solve is really that specific, you could do it even without using regular expression matches at all:

int lastDot = str.lastIndexOf(".");
columnArray[0] = str.substring(0, lastDot);
columnArray[1] = str.substring(lastDot + 1);
Thomas
  • 17,016
  • 4
  • 46
  • 70
3

As I posted on the original Post (here), the next code:

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

Produces the required output (an array with two values):

result: [myValue.Folder, FolderCentury]
Community
  • 1
  • 1
Tomas Narros
  • 13,390
  • 2
  • 40
  • 56