Python java equivalent string section

The Java string splitting function (regex) splits in all instances of the regular expression. The Python partition function only breaks into the first instance of this separator and returns a tuple {left, separator, right}.

How to achieve what section does in Java?

eg.

"foo bar hello world".partition(" ") 

should become

 "foo", " ", "bar hello world" 
  • Is there an external library that this utility already has?

  • how could i achieve this without an external library?

  • And can this be achieved without an external library and without Regex?

NB. I am not looking for split ("", 2) since it does not return a separator character.

+4
source share
5 answers

String.split(String regex, int limit) is close to what you want. From the documentation:

The limit parameter controls the number of uses of the template and, therefore, affects the length of the resulting array.

  • If the limit n greater than zero, the pattern will be applied no more than n - 1 times, the length of the array will be no more than n , and the last record of the array will contain all the input data for the last Corresponding separator.
  • If n not positive, the pattern will be applied as many times as possible, and the array can be of any length.
    • If n is zero, the template will be applied as many times as possible, the array can be of any length, and the final empty lines will be discarded.

Here is an example to show these differences ( as seen on ideone.com ):

 static void dump(String[] ss) { for (String s: ss) { System.out.print("[" + s + "]"); } System.out.println(); } public static void main(String[] args) { String text = "abcd---"; dump(text.split("-")); // prints "[a][b][c][d]" dump(text.split("-", 2)); // prints "[a][bcd---]" dump(text.split("-", -1)); // [a][b][c][d][][][] } 

The section containing the delimiter

If you need similar functionality for a section, and you also want to get a separator string that was matched by an arbitrary pattern, you can use Matcher , then take a substring with the corresponding indexes.

Here is an example ( as seen on ideone.com ):

 static String[] partition(String s, String regex) { Matcher m = Pattern.compile(regex).matcher(s); if (m.find()) { return new String[] { s.substring(0, m.start()), m.group(), s.substring(m.end()), }; } else { throw new NoSuchElementException("Can't partition!"); } } public static void main(String[] args) { dump(partition("james007bond111", "\\d+")); // prints "[james][007][bond111]" } 

The regular expression \d+ , of course, is any digit character ( \d ) repeated one or more times ( + ).

+5
source

While not quite what you need, there is a second version of split that takes the "limit" parameter, telling it the maximum number of partitions to split the string into.

So, if you called (in Java):

 "foo bar hello world".split(" ", 2); 

You will get an array:

 ["foo", "bar hello world"] 

which is larger or smaller than what you want, except for the fact that the delimiter character is not embedded in index 1. If you really need this last dot, you need to do it yourself, but hopefully everything that you specifically wanted it was possible to limit the number of splits.

+5
source

How about this:

 String partition(String string, String separator) { String[] parts = string.split(separator, 2); return new String[] {parts[0], separator, parts[1]}; } 

By the way, you need to add some input / result checks :)

+2
source

Using:

  "foo bar hello world" .split ("", 2)

The default separator is a space.

0
source

Is there an external library that already provides this utility?

No, of which I know.

how could I achieve this without an external library? And can this be achieved without an external library and without Regex?

Of course, this is not a problem at all; just use String.indexOf() and String.substring() . However, Java does not have a tuple data type, so you have to return an array, list, or write your own class of results.

0
source

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


All Articles