• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String objects are immutable!

try this:

 
Vinod Vijay
Ranch Hand
Posts: 165
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod Vijay wrote:Thank you all you guys...


You're welcome.

Winston
 
Police line, do not cross. Well, this tiny ad can go through:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic