5

If you have for example a list of tab-separated values:

foo1\tfoo2\tfoo3\tfoo4\t

The last \t was added due to automatic appending of the \t with each +=.

How do you remove that last \t in a easy way? So that the result is:

foo1\tfoo2\tfoo3\tfoo4

As a request from Hover, a small example of what I had:

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);

Output (replaced actual tab with stringed tab for display here):

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

That's the main reason I asked this question, .trim() wasn't working, therefore, I tough that trim() wasn't made for trailing tabs.

18
  • 4
    Please, please don't use += in a loop to build up a string. You basically end up with quadratic runtime.
    – Matt Ball
    Feb 6, 2013 at 4:25
  • 8
    Simply call trim() on the String. Feb 6, 2013 at 4:25
  • use lastIndexOf e.g. String c = "foo1\tfoo2\tfoo3\tfoo4\t"; c=c.subString(0,c.lanstIndexOf("\t")-1);
    – Satya
    Feb 6, 2013 at 4:25
  • No, use a better approach altogether. stackoverflow.com/questions/1751844/…
    – Matt Ball
    Feb 6, 2013 at 4:25
  • 1
    "\t" is not a whitespace which could be removed with trim()
    – asifsid88
    Feb 6, 2013 at 4:32

6 Answers 6

9
String s1 = "foo1\tfoo2\tfoo3\tfoo4\t".trim();
2
  • does "trim()" only remove "white-space"?
    – Henry
    Sep 19, 2017 at 8:29
  • @Henry trim() -> "Returns a string whose value is this string, with any leading and trailing whitespace removed." Nov 24, 2017 at 20:09
5
String expectedString = "foo1\tfoo2\tfoo3\tfoo4\t".trim();
4

If you just want to remove trailing tab(s), you could do this:

String s1 = "foo1\tfoo2\tfoo3\tfoo4\t";
while (s1.endsWith("\t")) {
    s1 = s1.substring(0, s1.length()-1);
}
2
  • +1 Nice idea :) - Might be better with a StringBuilder though, as this will create lots of small Strings Feb 6, 2013 at 4:40
  • nice!!! I was looking for this. Thanks! It gets rid of the exception I was getting using the other ways.
    – link_boy
    Feb 6, 2013 at 4:46
1

If your loop looks like this

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

You can

  • Use trim()
  • Use values.substring(0,values.length-1)
  • Modify your loop to do n-1 iterations and manually apply the last part without the tab
  • Add an explicit test for the nth iteration and not apply the "\t" (values += foo + (i==n-1)? numbers:numbers+"\t" )
1

HovercraftFullOfEels is right String#trim should do just what you want...

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 + "\"");
}

Which outputs...

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

Updated

And if that fails...something like...

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

Might help...

1

For clarification of our discussion, if you run this, what do you see?

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);
   }
}

Myself, I get:

"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"

showing a well-functioning trim() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.