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.

link_boy
  • 1,025
  • 4
  • 12
  • 23
  • 4
    Please, please don't use `+=` in a loop to build up a string. You basically end up with quadratic runtime. – Matt Ball Feb 06 '13 at 04:25
  • 8
    Simply call `trim()` on the String. – Hovercraft Full Of Eels Feb 06 '13 at 04:25
  • use lastIndexOf e.g. String c = "foo1\tfoo2\tfoo3\tfoo4\t"; c=c.subString(0,c.lanstIndexOf("\t")-1); – Satya Feb 06 '13 at 04:25
  • **No,** use a better approach altogether. http://stackoverflow.com/questions/1751844/java-convert-liststring-to-a-joind-string – Matt Ball Feb 06 '13 at 04:25
  • trim() should work. If not, you have a problem. Post a [SSCCE](http://sscce.org) reproducing the problem. – Cyrille Ka Feb 06 '13 at 04:31
  • 1
    "\t" is not a whitespace which could be removed with trim() – asifsid88 Feb 06 '13 at 04:32
  • @link_boy: then you're trying wrong. It should and will work if properly used. If it doesn't work for you, show your code in an edit so we can tell you how to fix it. – Hovercraft Full Of Eels Feb 06 '13 at 04:32
  • @asifsid88: please delete your nonsense comment. Next time, please test your suggestions before posting. – Hovercraft Full Of Eels Feb 06 '13 at 04:35
  • @link_boy: don't show code in comments as it's impossible to read. Please edit your question and show it there in the edit. – Hovercraft Full Of Eels Feb 06 '13 at 04:36
  • @link_boy `trim` works fine for me – MadProgrammer Feb 06 '13 at 04:36
  • @HovercraftFullOfEels don't really know, it wasn't working. I even created a new string that equals the oldstring.trim() and it didn't worked. I'm using openjdk. – link_boy Feb 06 '13 at 05:01
  • @link_boy: The best way to find out why it didn't work is to post a simple example String that ends with a tab, where you trim it, then print it out and show that it doesn't work. Again, without our seeing code, we can't solve this mystery. – Hovercraft Full Of Eels Feb 06 '13 at 05:11
  • @HovercraftFullOfEels did an example of what I had – link_boy Feb 06 '13 at 05:22
  • @link_boy: the trim in your example works fine. It won't work when the loop finishes because at that time, i will == 99 and is not divisible by 10. In that situation, you would do a trim *after* the loop. So as suggested, trim works, but you were using it wrong. Next time, please show us this when you ask your question or early in the process. Just telling us "it doesn't work" will not allow us to help you. – Hovercraft Full Of Eels Feb 06 '13 at 05:26
  • @HovercraftFullOfEels that was just using sample values, your comment doesn't explain why for example the first 9 iterations of tabbed-foo's end up with trailing /t. Also, I know that in the example I wrote the last iteration will end up with an /t. I will fix it to clear doubt. – link_boy Feb 06 '13 at 05:30
  • @link_boy: yes it works just fine. None of the lines ends with tab now. – Hovercraft Full Of Eels Feb 06 '13 at 05:32
  • @HovercraftFullOfEels I'm trying to help you see that it's not improper use. Sorry if this made you mad but trim() is really not working for me. – link_boy Feb 06 '13 at 05:35
  • Yikes! `\\t` is not the same as `\t`! `trim()` won't clear nonsense, just whitespace including tabs. – Hovercraft Full Of Eels Feb 06 '13 at 05:35

6 Answers6

9
String s1 = "foo1\tfoo2\tfoo3\tfoo4\t".trim();
Ren
  • 3,395
  • 2
  • 27
  • 46
5
String expectedString = "foo1\tfoo2\tfoo3\tfoo4\t".trim();
Yogesh Ralebhat
  • 1,376
  • 1
  • 13
  • 29
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);
}
Eddie
  • 427
  • 3
  • 8
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" )
dfb
  • 13,133
  • 2
  • 31
  • 52
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...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373