在java中删除字符串中尾随制表符的最简单方法

时间:2013-02-06 04:23:25

标签: java string formatting

如果您有一个以制表符分隔的值列表:

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()没有工作,因此,我很难修剪()没有用于尾随标签。

6 个答案:

答案 0 :(得分:7)

String s1 = "foo1\tfoo2\tfoo3\tfoo4\t".trim();

答案 1 :(得分:5)

String expectedString = "foo1\tfoo2\tfoo3\tfoo4\t".trim();

答案 2 :(得分:4)

如果您只想删除尾随标签,可以执行以下操作:

String s1 = "foo1\tfoo2\tfoo3\tfoo4\t";
while (s1.endsWith("\t")) {
    s1 = s1.substring(0, s1.length()-1);
}

答案 3 :(得分:1)

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

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

你可以

  • 使用trim()
  • 使用values.substring(0,values.length-1)
  • 修改循环以执行n-1次迭代,并手动应用不带标签的最后一部分
  • n次迭代添加显式测试,而不应用“\ t”(values += foo + (i==n-1)? numbers:numbers+"\t"

答案 4 :(得分: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() + "\"");

可能有帮助......

答案 5 :(得分: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()方法。