replacing tabs

Ok. I've tried a hundred different ways and I can't get it to work. How do I remove all the tabs in a string using replaceAll? I must not be getting the regular expression right.Thanks.
[201 byte] By [CSAngela] at [2007-11-26 23:33:02]
# 1
Here's number 101string.replaceAll("\\t", "");#
duckbilla at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 2
Don't forget strings are immutable -- the method returns the result:s = s.replace("\t", "");Again, the simpler replace will suffice when no regex patterns are involved.
DrLaszloJamfa at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 3

> Don't forget strings are immutable -- the method

> returns the result:

> > s = s.replace("\t", "");

>

> Again, the simpler replace will suffice when no regex

> patterns are involved.

I did that but it doesn't work.

CSAngela at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 4
thats because the doc forgot the 2nd \hey doc, "dont forget", lol:s = s.replace("\\t", "");(whats up with the teasing today, im outta hand!)
TuringPesta at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 5

Works like a charm for me:

public class ReplaceTest {

public static void main(String[] args) {

String s = "1\t2\t3";

s = s.replace("\t","");

System.out.println(s);

}

}

DrLaszloJamfa at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 6

> thats because the doc forgot the 2nd \

>

> hey doc, "dont forget", lol:

> s = s.replace("\\t", "");

>

> (whats up with the teasing today, im outta hand!)

replace() takes ordinary strings, not regular expression strings, so there is

no need to have the double backslash.

DrLaszloJamfa at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 7

> replace() takes ordinary strings, not regular

> expression strings, so there is

> no need to have the double backslash.

yikes. i sat here compiling your code and rubbing my eyes

wondering if i was crazy.

i just kept saying "i know its TWO backslashes".

i think i should call it a day. : /

TuringPesta at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 8
Ummm.... I should mention that this version of String's replace:String replace(CharSequence target, CharSequence replacement)was added in the previous version of the JDK (1.5).
DrLaszloJamfa at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 9
> I did that but it doesn't work.The String.replace(CharSequence, CharSequence) was added in JDK 1.5. So you are probably using JDK 1.4.2 (or earlier).
prometheuzza at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 10
What makes it confusing is that replaceAll() can take one or two backslashes. If you use one, the regex compiler sees a TAB character, and if you use two, it sees the escape for a TAB. With replace, you can only use the single-backslash version. The same goes for LF, CR, and FF.
uncle_alicea at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 11
> The String.replace(CharSequence, CharSequence)> was added in JDK 1.5. So you are probably using JDK> 1.4.2 (or earlier).To CSAngel:If you're not on JDK 1.5 or later, you can uses=s.replaceAll("\\t","");
KathyMcDonnella at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 12
> If you're not on JDK 1.5 or later, you can use> s=s.replaceAll("\\t","");What if the OP is using 1.3? ;-))
DrLaszloJamfa at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 13
> > If you're not on JDK 1.5 or later, you can use> > s=s.replaceAll("\\t","");> > What if the OP is using 1.3? ;-))1.3? Is this another story about your childhood Dr Jamf?; )
prometheuzza at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 14
... Those pre-ANSI C days were the happiest days of my life....
DrLaszloJamfa at 2007-7-11 14:52:02 > top of Java-index,Java Essentials,New To Java...
# 15
> > I did that but it doesn't work.> > The String.replace(CharSequence, CharSequence)> was added in JDK 1.5. So you are probably using JDK> 1.4.2 (or earlier).Nope. Using 6. But it doesn't want to work for some weird reason.
CSAngela at 2007-7-21 19:26:23 > top of Java-index,Java Essentials,New To Java...
# 16
> Nope. Using 6. But it doesn't want to work for some weird reason.Even the code in reply #5?
DrLaszloJamfa at 2007-7-21 19:26:23 > top of Java-index,Java Essentials,New To Java...
# 17
> ...> Nope. Using 6. But it doesn't want to work for some> weird reason.That doesn't say too much: if you'd post the code and the exact error message, I'm sure someone (perhaps even me) can point out the error in your code.
prometheuzza at 2007-7-21 19:26:23 > top of Java-index,Java Essentials,New To Java...
# 18

> > ...

> > Nope. Using 6. But it doesn't want to work for

> some

> > weird reason.

>

> That doesn't say too much: if you'd post the code and

> the exact error message, I'm sure someone (perhaps

> even me) can point out the error in your code.

Doh! I just discovered I had more than one debug session running in NetBeans. I think they may have been part of the problem. I'm going to try it again.

I had already tried

s.replace("\t","");

s.replace("\\t","");

s.replaceAll("\t","");

s.replaceAll("\\t","");

before I posted this question....

CSAngela at 2007-7-21 19:26:23 > top of Java-index,Java Essentials,New To Java...
# 19

> ...

> s.replace("\t","");

> s.replace("\\t","");

> s.replaceAll("\t","");

> s.replaceAll("\\t","");

> ...

1 and 4 will work, 2 and 3 won't.

As already mentioned: both mehods return a new String, so you'll have to do eithers = s.replace("\t","");

ors = s.replaceAll("\\t","");

prometheuzza at 2007-7-21 19:26:23 > top of Java-index,Java Essentials,New To Java...
# 20

> > ...

> > s.replace("\t","");

> > s.replace("\\t","");

> > s.replaceAll("\t","");

> > s.replaceAll("\\t","");

> > ...

>

> 1 and 4 will work, 2 and 3 won't.

> As already mentioned: both mehods return a new

> String, so you'll have to do eithers =

> s.replace("\t","");

ors =

> s.replaceAll("\\t","");

Ok. It's working now. Anyone know how I can tell when more than one debug session is running in NetBeans? It's easy in Eclipse. ;-(

CSAngela at 2007-7-21 19:26:23 > top of Java-index,Java Essentials,New To Java...
# 21

> ... Those pre-ANSI C days were the happiest days of

> my life....

All I remember specifically is pre-ANSI C++ and when one compiler author told me that his compiler worked in a way that contradicted "C++ Programming Language" because he didn't agree with the way Stroustrup wanted C++ to work.

It wasn't too surprising to me that Stroustrup's version won.

jschella at 2007-7-21 19:26:23 > top of Java-index,Java Essentials,New To Java...