toString方法删除太多的空格

时间:2016-03-20 02:43:59

标签: java stack tostring

我有一个带有2个堆栈(leftright)的文本编辑器缓冲区hw赋值。一切都按照它应该在大多数情况下的方式运作。但我遇到的麻烦是,它返回了太多的空白区域。我特意尝试填写toString()方法来返回文本。 例如,返回文本打印:

 T h e r e i s g r a n d e u r i n t h i s v i e w o f l i f e ,

字母之间有单个空格,每个单词之间有双重空格。如何删除字母之间的空格,同时只删除单词之间的1个空格,以便我的字符串返回:

There is grandeur in this view of life,
public class Buffer {
    private Stack<Character> left;  // chars left of cursor
    private Stack<Character> right; // chars right of cursor

    // Create an empty buffer.
    public Buffer() {
        left = new Stack<Character>();
        right = new Stack<Character>();
    }

    // Insert c at the cursor position.
    public void insert(char c) {
        left.push(c);
    }

    // Delete and return the character at the cursor.
    public char delete() {
        if (!right.isEmpty()){
            return right.pop();
        }else return 0;
    }

    // Move the cursor k positions to the left.
    public void left(int k) {
        while (!left.isEmpty() && --k >= 0){
            right.push(left.pop());
        }
    }

    // Move the cursor k positions to the right.
    public void right(int k) {
        while (!right.isEmpty() && --k >=0){
            left.push(right.pop());
        }
    }

    // Return the number of characters in the buffer.
    public int size() {
        return left.size()+right.size();
    }

    // Return a string representation of the buffer with a "|" character (not 
    // part of the buffer) at the cursor position.
    public String toString() {

        String a = (left+"|"+right);


        return a;
    }

    // Test client (DO NOT EDIT).
    public static void main(String[] args) {
        Buffer buf = new Buffer();
        String s = "There is grandeur in this view of life, with its " 
            + "several powers, having been originally breathed into a few " 
            + "forms or into one; and that, whilst this planet has gone " 
            + "cycling on according to the fixed law of gravity, from so " 
            + "simple a beginning endless forms most beautiful and most " 
            + "wonderful have been, and are being, evolved. ~ " 
            + "Charles Darwin, The Origin of Species";
        for (int i = 0; i < s.length(); i++) {
            buf.insert(s.charAt(i));
        }
        buf.left(buf.size());
        buf.right(97);
        s = "by the Creator ";
        for (int i = 0; i < s.length(); i++) {
            buf.insert(s.charAt(i));
        }
        buf.right(228);
        buf.delete();
        buf.insert('-');
        buf.insert('-');
        buf.left(342);
        StdOut.println(buf);
    }
}

2 个答案:

答案 0 :(得分:1)

您的代码依赖于toString()的{​​{1}}方法。您不应该这样做,除非格式定义明确,并且Stack没有明确定义,尽管它的输出方式与其他集合类相同,即java.util.Stack

有关输出的示例,请参阅this IDEONE(一旦我将[value1, value2, value3]替换为StdOut):

System.out

正如人们告诉你的那样,整个字符串是相反的,并且有括号([]|[s, e, i, c, e, p, S, , f, o, , n, i, g, i, r, O, , e, h, T, , ,, n, i, w, r, a, D, , s, e, l, r, a, h, C, , -, -, , ., d, e, v, l, o, v, e, , ,, g, n, i, e, b, , e, r, a, , d, n, a, , ,, n, e, e, b, , e, v, a, h, , l, u, f, r, e, d, n, o, w, , t, s, o, m, , d, n, a, , l, u, f, i, t, u, a, e, b, , t, s, o, m, , s, m, r, o, f, , s, s, e, l, d, n, e, , g, n, i, n, n, i, g, e, b, , a, , e, l, p, m, i, s, , o, s, , m, o, r, f, , ,, y, t, i, v, a, r, g, , f, o, , w, a, l, , d, e, x, i, f, , e, h, t, , o, t, , g, n, i, d, r, o, c, c, a, , n, o, , g, n, i, l, c, y, c, , e, n, o, g, , s, a, h, , t, e, n, a, l, p, , s, i, h, t, , t, s, l, i, h, w, , ,, t, a, h, t, , d, n, a, , ;, e, n, o, , o, t, n, i, , r, o, , s, m, r, o, f, , w, e, f, , a, , o, t, n, i, , r, o, t, a, e, r, C, , e, h, t, , y, b, , d, e, h, t, a, e, r, b, , y, l, l, a, n, i, g, i, r, o, , n, e, e, b, , g, n, i, v, a, h, , ,, s, r, e, w, o, p, , l, a, r, e, v, e, s, , s, t, i, , h, t, i, w, , ,, e, f, i, l, , f, o, , w, e, i, v, , s, i, h, t, , n, i, , r, u, e, d, n, a, r, g, , s, i, , e, r, e, h, T] )和逗号空格分隔符([])。

如果你没有看到,那么你可能不会使用,,而是使用一些自行开发的实现。

无论如何,解决方案是修复您的java.util.Stack实施不依赖于toString的{​​{1}},因为即使它没有输出空格,您仍然会变坏结果如果“光标”位于文本的中间。

如果您使用的是toString(),则应创建Stack,然后重复java.util.Stack并附加每个字符,附加StringBuilder(),然后重复left使用|并使用rightright.listIterator(right.size())向后迭代以追加字符。

答案 1 :(得分:0)

修复你的算法(首选),或者这是一个简单的快速和肮脏的#34;方式:

String fixed = str.replaceAll("(?<! ) ", "");

匹配正则表达式表示&#34;空格字符前面没有空格字符&#34;。