Split string in Java versus split rule?

I have a line like this:

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

Is it possible to split the above line into . but instead of getting three resulting rows there are only two types:

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

Or do I need to use another java method to do this?

-one
source share
3 answers

As I posted on the original Post ( here ), the following 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] 
+3
source

Try it.

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

Hi programmer / Yannish,

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

+4
source

If the problem you are trying to solve is really like this, you can do it without even using regular expressions:

 int lastDot = str.lastIndexOf("."); columnArray[0] = str.substring(0, lastDot); columnArray[1] = str.substring(lastDot + 1); 
+3
source

Source: https://habr.com/ru/post/1499582/


All Articles