Skip to content

Join and split strings in Java like in Python

aburkov edited this page Jun 27, 2015 · 1 revision

With xpresso you can join and split strings in the pythonic way.

Python:

colorsPattern = "|".join(["black","green","red","white"]);

print colorsPattern

>>> black|green|red|white

xpresso:

String colorsPattern = x.String("|").join(x.list("black","green","red","white"));

x.print(colorsPattern);

Console: black|green|red|white

Python:

tokens = "Moscow;London;Paris".split(";")

print tokens

>>> ['Moscow', 'London', 'Paris']

xpresso:

list<String> tokens = x.String("Moscow;London;Paris").split(";");

x.print(tokens);

Console: [Moscow, London, Paris]

The "in" operation on strings is also available:

Python:

if "e" in "Hello World":
    #do stuff

xpresso:

if(x.String("e").in("Hello World")) {
    //do stuff
}