简体   繁体   中英

Splitting a string between a char

I want to split a String on a delimiter. Example String:

String str="ABCD/12346567899887455422DEFG/15479897445698742322141PQRS/141455798951";

Now I want Strings as ABCD/12346567899887455422 , DEFG/15479897445698742322141 like I want

  • only 4 chars before /
  • after / any number of chars numbers and letters. Update: The only time I need the previous 4 characters is after a delimiter is shown, as the string may 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");
    }

}

You claim that after / there can appear digits and alphabets, but in your example I don't see any alphabets which should be included in result after / .

So based on that assumption you can simply split in placed which has digit before and AZ character after it.

To do so you can split with regex which is using look-around mechanism like str.split("(?<=[0-9])(?=[AZ])")

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 you alphabets can actually appear in second part (after / ) then you can use split which will try to find places which have four alphabetic characters and / after it like split("(?=[AZ]{4}/)") (assuming that you are using at least Java 8, if not you will need to manually exclude case of splitting at start of the string for instance by adding (?!^) or (?<=.) at start of your regex).

you can use regex

    Pattern pattern = Pattern.compile("[A-Z]{4}/[0-9]*");
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
      System.out.println(matcher.group());
    }

Instead of:

String[] codeDesc = path.split("/");

Just use this regex (4 characters before / and any characters after):

String[] codeDesc = path.split("(?=.{4}/)(?<=.)");

Even simpler using \\d :

path.split("(?=[A-Za-z])(?<=\\\\d)");

EDIT:

Included condition for 4 any size letters only.

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 still unclear if this is authors expected result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM