• 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 example problem

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I just started in java and I'm working through Sams Teach Yourself Java 1.2 in 21 Days. There's an example that uses string methods like indexOf, charAt and others. Well there's one that I'm confused on and hoped someone could shed some light on for me. Here's the code.

String str = "In my next life, I will believe in reincarnation";

System.out.println("The index of the beginning of the " + "substring \"will\": + str.indexOf("will"));
I understand it's using the indexOf method, but my question is why does it break up ...."of the" + "substring \"will\": into two strings there? I assume the quotes around will have something to do with it, but I'm not really clear on it all. Thanks for any insight you can give.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The \" is used to print " in your output.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tyler,
You seem to be missing some quotes there - I'm guessing that what you meant is:-
System.out.println("The index of the beginning of the " + "substring \"will\": "+ str.indexOf("will"));
I've added " before the final +.
I don't think there is any good reason for splitting up the string. It's equivalent to:-
System.out.println("The index of the beginning of the substring \"will\": "+ str.indexOf("will"));
It may simply be to improve the format of the code. If the code makes the line too long, you can spread it over two lines if you use + to concatenate the strings.
Hope this helps,
Kathy

[This message has been edited by Kathy Rogers (edited December 01, 2000).]
 
tyler jones
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I couldn't figure out why they would do that, so I'm glad to hear that there's really no reason and I don't have to worry about it.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tyler,
The '\' is known as escape character which is used when you need to override the chars that have spceial meaning in java, int this case '"'.
Here you are also missing a quote just before + str.
In your example the output would be :
The index of the beginning of the substring "will" : <some number>

 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic