link_boy
link_boy

Reputation: 1075

Easiest way to remove trailing tab in a string in java

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.

Upvotes: 5

Views: 19231

Answers (6)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347294

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

Upvotes: 1

dfb
dfb

Reputation: 13289

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

Upvotes: 1

Eddie
Eddie

Reputation: 427

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

Upvotes: 4

Yogesh Ralebhat
Yogesh Ralebhat

Reputation: 1466

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

Upvotes: 5

Ren
Ren

Reputation: 3455

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

Upvotes: 9

Related Questions