Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialannient
Courses Plus Student 1,708 PointsWhat does String.format do?
I am confused with this line in method toString() of Song.java file: return String.format("Song: %s by %s", mTitle, mArtist); What does String.format do? Please help :)
1 Answer
Evan Demaris
64,262 PointsHi annient,
String.format()
takes a string as an argument that can be formatted using flags. %s
is string format, meaning that after the initial flagged string, it expects you to define what %s
is going to be replaced with after the string; these statements are functionally similar:
String.format("Song: %s by %s", mTitle, mArtist);
"Song: " + mTitle + " by " + mArtist;
Hope that helps!
annient
Courses Plus Student 1,708 Pointsannient
Courses Plus Student 1,708 PointsThanks Evan for your explanation. It helps me a lot :)