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.
source to share
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"
)
source to share
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...
source to share
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.
source to share