简体   繁体   English

在char之间拆分字符串

[英]Splitting a string between a char

I want to split a String on a delimiter. 我想在分隔符上拆分一个String。 Example String: 示例字符串:

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

Now I want Strings as ABCD/12346567899887455422 , DEFG/15479897445698742322141 like I want 现在我想要像我想要的字符串ABCD/12346567899887455422DEFG/15479897445698742322141

  • only 4 chars before / 之前只有4个字符/
  • 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... 更新:我需要前4个字符的唯一时间是在显示分隔符后,因为字符串可能包含字母或数字...

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. 因此,基于该假设,您可以简单地拆分前面有数字的位置和后面的AZ字符。

To do so you can split with regex which is using look-around mechanism like str.split("(?<=[0-9])(?=[AZ])") 为此,您可以使用正则表达式进行split ,该正则表达式使用str.split("(?<=[0-9])(?=[AZ])")外观机制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). 如果你的字母实际上可以出现在第二部分(在/之后),那么你可以使用split来尝试找到有四个字母字符的地方和/之后像split("(?=[AZ]{4}/)") (假设您至少使用Java 8,如果不是,则需要在字符串的开头手动排除拆分的情况,例如在正则表达式的开头添加(?!^)(?<=.) )。

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): 只需使用此正则表达式(之前的4个字符和之后的任何字符):

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

Even simpler using \\d : 使用\\d更简单:

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

EDIT: 编辑:

Included condition for 4 any size letters only. 包含4个任何尺寸字母的条件。

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. 目前尚不清楚这是否是作者预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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