Splitting a string between char
I want to split String at delimiter. Example String:
String str="ABCD/12346567899887455422DEFG/15479897445698742322141PQRS/141455798951";
Now I want to have a line ABCD/12346567899887455422
, DEFG/15479897445698742322141
as I want
- just 4 characters before
/
- after / any number of characters and letters. Update: The only time I need the previous 4 characters is after displaying the delimiter, since the string can contain letters or numbers ...
My code attempt:
public class StringReq {
public static void main(String[] args) {
String str = "BONL/1234567890123456789CORT/123456789012345678901234567890HOLD/123456789012345678901234567890INTC/123456789012345678901234567890OTHR/123456789012345678901234567890PHOB/123456789012345678901234567890PHON/123456789012345678901234567890REPA/123456789012345678901234567890SDVA/123456789012345678901234567890TELI/123456789012345678901234567890";
testSplitStrings(str);
}
public static void testSplitStrings(String path) {
System.out.println("splitting of sprint starts \n");
String[] codeDesc = path.split("/");
String[] codeVal = new String[codeDesc.length];
for (int i = 0; i < codeDesc.length; i++) {
codeVal[i] = codeDesc[i].substring(codeDesc[i].length() - 4,
codeDesc[i].length());
System.out.println("line" + i + "==> " + codeDesc[i] + "\n");
}
for (int i = 0; i < codeVal.length - 1; i++) {
System.out.println(codeVal[i]);
}
System.out.println("splitting of sprint ends");
}
}
source to share
You argue that /
numbers and alphabets can appear after , but in your example, I don't see alphabets that should be included in the result after /
.
So, based on this assumption, you can simply split the layouts that have numbers before and after AZ.
To do this you can split
with a regex that uses a look-around mechanism , for examplestr.split("(?<=[0-9])(?=[A-Z])")
Demo:
String str = "BONL/1234567890123456789CORT/123456789012345678901234567890HOLD/123456789012345678901234567890INTC/123456789012345678901234567890OTHR/123456789012345678901234567890PHOB/123456789012345678901234567890PHON/123456789012345678901234567890REPA/123456789012345678901234567890SDVA/123456789012345678901234567890TELI/123456789012345678901234567890";
for (String s : str.split("(?<=[0-9])(?=[A-Z])"))
System.out.println(s);
Output:
BONL/1234567890123456789 CORT/123456789012345678901234567890 HOLD/123456789012345678901234567890 INTC/123456789012345678901234567890 OTHR/123456789012345678901234567890 PHOB/123456789012345678901234567890 PHON/123456789012345678901234567890 REPA/123456789012345678901234567890 SDVA/123456789012345678901234567890 TELI/123456789012345678901234567890
If the alphabet can appear in the second part (after /
), you can use split, which will try to find places with and /
after four alphabetic characters , like split("(?=[A-Z]{4}/)")
(assuming you are using at least Java 8, unless you need to manually exclude the case of splitting at the beginning of a line, like adding (?!^)
or (?<=.)
at the beginning of your regex).
source to share
It's even easier to use \d
:
path.split("(?=[A-Za-z])(?<=\\d)");
EDIT:
Only 4 letters of any size included.
path.split("(?=[A-Za-z]{4})(?<=\\d)");
output:
BONL/1234567890123456789 CORT/123456789012345678901234567890 HOLD/123456789012345678901234567890 INTC/123456789012345678901234567890 OTHR/123456789012345678901234567890 PHOB/123456789012345678901234567890 PHON/123456789012345678901234567890 REPA/123456789012345678901234567890 SDVA/123456789012345678901234567890 TELI/123456789012345678901234567890
It is not yet clear if this is the expected result for the authors.
source to share