Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String methods - replace()  RSS feed

 
Vijeyalakshmi Narasimhan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String start = "AniMaL   ";
String trimmed = start.trim();                 // "AniMaL"
String lowercase = trimmed.toLowerCase();      // "animal"
String result = lowercase.replace('a', 'A');   // "Animal"  -----> Shdn't both the letter 'a' be replaced by "A"
System.out.println(result);

What am i missing here?!
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What output did you get when you ran this code?  I got AnimAl.
 
Vijeyalakshmi Narasimhan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup got the same as yours !! visited previous post

http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-char-char-

public String replace(char oldChar,
                      char newChar)
Parameters:
oldChar - the old character.
newChar - the new character.
Returns:
a string derived from this string by replacing every occurrence of oldChar with newChar.


So i think (AnimAl) is the corrrect output !! waiting for the input from others....
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why would the answer be other than AnimAl? Did you try the code yourself, or did you only read the documentation?

I shall take the liberty of moving this discussion since it is not specific to Java8.
 
Vijeyalakshmi Narasimhan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the code and it turned out to be "AnimAl" both A's replaced !!

Im following the book [OCA: Oracle Certified Associate Java SE 8 Programmer I Study Guide: Exam 1Z0-808
By: Jeanne Boyarsky; Scott Selikoff ]- chapter 3 - String methods - replace() - "Animal" with only the first A seems to be replaced !!

Referred previous docs and found the link  http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-char-char-  ;
which says it will replace every occurence of old char with new char!!

Wondering am i right ?that it replaces every occurence of old char with new char !!
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijeyalakshmi Narasimhan wrote:Wondering am i right ?that it replaces every occurence of old char with new char !!
Yes you are correct. It is errata, already addressed here is the link of that errata discussion and you can see lists of errata of Java OCA 8 Programmer I Study Guide by Jeanne Boyarsky and Scott Selikoff already displayed here.
 
Vijeyalakshmi Narasimhan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much for the clarification and links 
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!