"装饰" Java中的String,带有基于位置的字符

时间:2014-05-28 11:14:17

标签: java text

我有一个文本字符串,以及一个位置对列表,用于定义文本中我需要“装饰”(或注释)的跨距。例如:

String t1 = "I saw Bob, and also Mary, John's sister.";
int[][] pos1 = {{6, 9}, {20, 39}, {26, 30}};

预期产出:

"I saw [Bob], and also [Mary, [John]'s sister]."

重点是:

  1. 这些位置是指原始文字。因此,在添加第一个'['后,其余位置变为无效,必须手动更新。

  2. 跨度可能重叠(如“约翰”和“玛丽,约翰的姐姐”)。所以在添加'['之后,更新其他位置并非易事。

  3. 我想我可以实现这些更新,但它看起来相当复杂,有很多索引簿记和边缘情况。是否有任何现有的类执行此任务?

3 个答案:

答案 0 :(得分:1)

public static void main(String[] args) {
    String t1 = "I saw Bob, and also Mary, John's sister.";
    int[][] pos1 = {{6, 9}, {20, 39}, {26, 30}};

    Map<Integer, String> map = new TreeMap<>();
    for (int i=0; i<pos1.length; i++) {
        map.put(pos1[i][0], "[");
        map.put(pos1[i][1], "]");
    }

    StringBuilder result = new StringBuilder();
    int firstIndex=0;
    for (int i: map.keySet()) {
        result.append(t1.substring(firstIndex, i)).append(map.get(i));
        firstIndex = i;
    }
    result.append(t1.substring(firstIndex));
    System.out.println(result);
}

答案 1 :(得分:1)

用于工作双支架。

字符串t1 =“我看到了鲍勃,还有玛丽,约翰的姐姐。”;

int [] [] pos1 = {{6,9},{20,39},{26,30},{26,30}};

结果“我看到了[鲍勃],还有[玛丽,[[约翰]]的姐姐。”

public class Record implements Comparable<Record> {
    private int index;
    private String value;

    public Record(int index, String value) {
        this.index = index;
        this.value = value;
    }

    @Override
    public int compareTo(Record o) {
        return index - o.index;
    }

    public int getIndex() {
        return index;
    }

    public String getValue() {
        return value;
    }

    public static void main(String[] args) {
        String t1 = "I saw Bob, and also Mary, John's sister.";
        int[][] pos1 = { { 6, 9 }, { 20, 39 }, { 26, 30 }, { 26, 30 } };

        List<Record> records = new ArrayList<>();
        for (int i = 0; i < pos1.length; i++) {
            records.add(new Record(pos1[i][0], "["));
            records.add(new Record(pos1[i][1], "]"));
        }

        Collections.sort(records);

        StringBuilder result = new StringBuilder();
        int firstIndex = 0;
        for (Record r : records) {
            result.append(t1.substring(firstIndex, r.getIndex())).append(
                    r.getValue());
            firstIndex = r.getIndex();
        }
        result.append(t1.substring(firstIndex));
        System.out.println(result);
    }
}

答案 2 :(得分:0)

这样的事情 - 仍然有一些优化的空间,但总体思路就在这里。我认为它非常易读。

private String getDecorator(int charIndex, int[][] pos) {
    for (int i = 0; i < pos.length; i++) {
        if (pos[i][0] == charIndex) {
            return "[";
        }
        if (pos[i][1] == charIndex) {
            return "]";
        }
    }
    return "";
}

public String decorate(String text, int[][] pos) {
    final StringBuffer sb = new StringBuffer();

    for (int charIndex=0; charIndex < text.length(); charIndex++) {
        sb.append(getDecorator(charIndex, pos));
        sb.append(text.charAt(charIndex));
    }

    return sb.toString();
}