• 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

String methods

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please help me to teach the replace that takes three arguments.Is this mock question correct

what will be the outputs of the following code

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not stick it in the compiler and see what it does?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A is incorrect because you cannot use the "-" operator on strings, and also the replace method for strings has a different syntax.
B is correct. The replace method for StringBuffer takes three arguments. Here's the method prototype:
StringBuffer replace (int start, int end, String str)
start is the starting character index, end - 1 is the last character index, str is a string to substitute instead of the denoted characters.
So after s.delete(0,3) the StringBuffer has "456789", after .replace(1,3,"24") - "424789", after .delete(4,6) - "4247".
C and D is incorrect because the substring method returns an object of type String which does not have a delete method, so the method chaining fails here.
E is incorrect because the replace method in StringBuilder takes 3 arguments.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic