5

例如,如果您有一个制表符分隔值列表:

foo1\tfoo2\tfoo3\tfoo4\t

最后一个\t是由于自动将\t附加到每个+=.

你如何以简单的方式删除最后一个\t ?所以结果是:

foo1\tfoo2\tfoo3\tfoo4

作为 Hover 的请求,我有一个小例子:

String foo = "";
for (int i = 1; i <= 100; i++) {
    foo += "foo" + "\t";
    if (i % 10 == 0) {
        foo = foo.trim(); // wasn't working
        foo += "\n";
    }
}
System.out.println(foo);

输出(用字符串选项卡替换实际选项卡以在此处显示):

foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t
foo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\tfoo\t

这就是我问这个问题的主要原因, .trim() 不起作用,因此,我坚持认为 trim() 不是为尾随制表符制作的。

4

6 回答 6

9
String s1 = "foo1\tfoo2\tfoo3\tfoo4\t".trim();
于 2013-02-06T04:29:03.350 回答
5
String expectedString = "foo1\tfoo2\tfoo3\tfoo4\t".trim();
于 2013-02-06T04:32:32.460 回答
4

如果您只想删除尾随制表符,您可以这样做:

String s1 = "foo1\tfoo2\tfoo3\tfoo4\t";
while (s1.endsWith("\t")) {
    s1 = s1.substring(0, s1.length()-1);
}
于 2013-02-06T04:38:24.017 回答
1

如果你的循环看起来像这样

for(...){
     values += foo + number + "\t" 
}

你可以

  • 利用trim()
  • 使用 values.substring(0,values.length-1)
  • 修改循环以进行n-1迭代并手动应用没有选项卡的最后一部分
  • 为第 th 次迭代添加显式测试,n而不应用 "\t" ( values += foo + (i==n-1)? numbers:numbers+"\t")
于 2013-02-06T04:38:28.087 回答
1

HovercraftFullOfEels 是正确的String#trim应该做你想做的......

String testing = "foo1\tfoo2\tfoo3\tfoo4\t";
System.out.println("\"" + testing.trim() + "\"");
if (testing.endsWith("\t")) {
    testing = testing.substring(0, testing.lastIndexOf("\t"));
    System.out.println("\"" + testing + "\"");
}

哪个输出...

"foo1   foo2    foo3    foo4"
"foo1   foo2    foo3    foo4"

更新

如果那失败了......就像......

StringBuilder sb = new StringBuilder(testing);
while (sb.lastIndexOf("\t") == sb.length()) {
    sb.delete(sb.length() - 1, sb.length());
}
System.out.println("\"" + sb.toString() + "\"");

可能有帮助...

于 2013-02-06T04:39:52.337 回答
1

为了澄清我们的讨论,如果你运行这个,你会看到什么?

public class Foo3 {
   public static void main(String[] args) {
      String foo = "";
      for (int i = 1; i <= 100; i++) {
         if (i % 10 == 1) {
            foo += "\"";
         }

         foo += "foo" + "\t";
         if (i % 10 == 0) {
            foo = foo.trim(); // wasn't working
            foo += "\"\n";
         }
      }
      System.out.println(foo);
   }
}

我自己,我得到:

“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”
“foo foo foo foo foo foo foo foo foo foo”

展示了一个运行良好的 trim() 方法。

于 2013-02-06T05:42:51.470 回答