FAQ: String Methods - substring()

This community-built FAQ covers the “substring()” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise substring()

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello! While reading this lesson I found the wording describing the use of substring() with two parameters to be misleading, as if I’m not mistaken the first parameter indicates the index of the first character of the string to extract, while the second parameter indicates the index of the character right after the last character of the substring.

In the given exercise, 4 is the index of the letter ‘H’ while 11 is the index of the space after the word, while the wording seems to indicate one would need to use 10, the index of the letter ‘s’. I just wanted to flag this in case others are finding this part to be confusing.

1 Like

I’m confusing that isn’t the below substring comes under range of (3,9)?
Java substring

I think you’ve miscounted. Remember that indexes start at 0 and that substring(int beginIndex, int endIndex) will return a string between beginIndex and endIndex, including the character at beginIndex but excluding the character at endIndex.

1 Like

it starts with 0, I agree it is confusing, but if you put in 0, then you don’t leave a single letter out. a substring with 0 would just print out the entire message. So to put The out we need to go to 3 and for The_ we need 4

Codecademy doesn’t accept my right code which is

public class Poetry {
  
	public static void main(String[] args) {
      
    String line = "The Heav'ns and all the Constellations rung";     
    // Change the arguments:
    System.out.println(line.substring(line.indexOf("H"), line.indexOf(" an")));
    
  }
  
}

Please consider fixing this issue in future updates

this code run as well: System.out.println(line.substring(line.indexOf(“H”),(line.indexOf(“s”)+1)));