Easiest way to remove trailing tab on line in java

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

foo1\tfoo2\tfoo3\tfoo4\t

      

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

.

How can I remove this last \ t in a simple manner? So 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 (replace the actual tab with the tab string to 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

      

Which is the main reason I asked this question, .trim () didn't work, so I hard-code that trim () was not done for trailing tabs.

+3


source to share


6 answers


If you just want to remove the trailing tabs, you can do this:



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

      

+4


source


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

      



+6


source


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

      

+5


source


If your loop looks like this:

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

      

You can

  • Use trim()

  • Use values ​​.substring (0, values.length-1)
  • Change your loop to iterate n-1

    and manually apply the last part without the tab
  • Add an explicit test for the n

    th iteration and not apply "\ t" ( values += foo + (i==n-1)? numbers:numbers+"\t"

    )
+1


source


HovercraftFullOfEels correctly String#trim

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

      

What are the outputs ...

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

      

Update

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

      

May I help...

+1


source


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

      

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

+1


source







All Articles