如何从byte []获取String及其行位置

时间:2014-02-06 11:09:23

标签: java regex

假设我有一个如下字符串:

static String MSG = "4: \n"+
        ":xx:xyz\n"+
        ":xx:abcdef\n"+ 
        ":xx:axvavsba\n"+ 
        ":xx:/000000000000\n"+ 
        ":xxs:/abcssd\n"+ 
        "efg\n"+ 
        "ijk\n"+ 
        "lmn\n"+ 
        "pqr\n"+ 
        ":xx:asasasasas";

从此我想转换成

byte[] messageBytes = MSG.getBytes("utf-8");

并且从messageBytes,我想根据位置及其行号提取字符串

例如,如果我要给extract(startIndex, endIndex)它应该返回该位置的原始字符串及其行号

此处我引用的行号是::内的每个字符串都是一行号

我尝试使用

String str = new String(ArrayUtils.subarray(messageByte, startIndex, endIndex), "utf-8");

我可以检索字符串但是如何提取相应的行号。

任何有关此问题的帮助都会被批评。

这样做的目的是作为一个字符串可能会受到一些japanese字符的影响,所以如果我将应用regex那么我将无法得到确切的字符串,以便完成同样的工作我正在尝试这种方法。

任何建议或新方法都会很好。

1 个答案:

答案 0 :(得分:0)

示例代码

System.out.println(findStringAndPosition(MSG));

输出

xx found at line 2
xx found at line 3
xx found at line 4
xx found at line 5
xxs found at line 6
xx found at line 11

详细信息

public static Informations findStringAndPosition(String input) {
    Informations infos = new Informations();
    String[] lines = input.split("\n");
    Pattern p = Pattern.compile("^:([^:]+):.*$");
    Matcher m = p.matcher("");

    int lineNumber = 0;
    for (String line : lines) {
        lineNumber++;
        m.reset(line);
        if (m.find()) {
            infos.add(new Info(lineNumber, m.group(1)));
        }
    }

    return infos;
}

static class Informations extends ArrayList<Info> {
    private static final long serialVersionUID = -2872174623287128687L;

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        for (Info info : this) {
            sb.append(String.format("%s found at line %s\n", info.getString(), info.getPosition()));
        }

        return sb.toString();
    }
}

static class Info {
    private String string;
    private int position;

    public Info(int position, String string) {
        this.string = string;
        this.position = position;
    }

    public final String getString() {
        return string;
    }

    public final int getPosition() {
        return position;
    }
}