• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding tab(\t) character in a sample java program.  RSS feed

 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code below. I want to look for any tab(\t) character in a string and if it has tab then replace it by a space and print the string without tab on console. But it is not working. Still getting tab at the end. Any idea?

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable, which is why the return() method returns a new String object with the modified value.
Your code sample ignores this return value.
 
Stas Melnychenko
Greenhorn
Posts: 14
Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String objects are immutable!

try this:

 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:Strings are immutable, which is why the return() method returns a new String object with the modified value.
Your code sample ignores this return value.


Thanks, but may I know what do you mean by "String objects are immutable! " in a layman language.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:Thanks, but may I know what do you mean by "String objects are immutable! " in a layman language.

It means that you can't change them.

And what that means in programming terms is that
name1.replace("\t", " ");
does NOT change name1. It returns a new String, with the contents replaced.

HIH

Winston
 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Vinod Vijay wrote:Thanks, but may I know what do you mean by "String objects are immutable! " in a layman language.

It means that you can't change them.

And what that means in programming terms is that
name1.replace("\t", " ");
does NOT change name1. It returns a new String, with the contents replaced.

HIH

Winston


I'm sorry, but I feel like there is a condraction in your statement. What you said was alright and I get that but in your last statement, you said
It returns a new String, with the contents replaced
Does'nt that mean that although a new string object was returned but old characters(\t) were replaced by new characters(<space>) in that?
please explain
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:
Jelle Klap wrote:Strings are immutable, which is why the return() method returns a new String object with the modified value.
Your code sample ignores this return value.


Thanks, but may I know what do you mean by "String objects are immutable! " in a layman language.


Sure.
The API documentation of the String class will tell you:
Strings are constant; their values cannot be changed after they are created.


That's basically what immutability boils down to. More generally speaking (not limited to Strings): any object who's state can't be altered/modified/mutated after it has been created can be called immutable.
So another fancy word for a rather simple concept.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:What you said was alright and I get that but in your last statement, you said
It returns a new String, with the contents replaced
Does'nt that mean that although a new string object was returned but old characters(\t) were replaced by new characters(<space>) in that?
please explain

You've got it in one. The method does exactly what it says it will do, but what you get back is a different String.
So, as Stas already pointed out, if you want name1 to reflect the new value, you must put
name1 = name1.replace("\t", " ");

With the exception of the increment operator, the same is also true of primitives.
If you want to add 46 to an int, you can't just write
int i;
i + 46;

you have to write
i = i + 46;

Winston
 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Vinod Vijay wrote:What you said was alright and I get that but in your last statement, you said
It returns a new String, with the contents replaced
Does'nt that mean that although a new string object was returned but old characters(\t) were replaced by new characters(<space>) in that?
please explain

You've got it in one. The method does exactly what it says it will do, but what you get back is a different String.
So, as Stas already pointed out, if you want name1 to reflect the new value, you must put
name1 = name1.replace("\t", " ");

With the exception of the increment operator, the same is also true of primitives.
If you want to add 46 to an int, you can't just write
int i;
i + 46;

you have to write
i = i + 46;

Winston




Thank you all you guys...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:Thank you all you guys...

You're welcome.

Winston
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!