Lesson 22, substrings


#1

Exercise

I keep gettting an error code that I do not have a substring in the code. What am I doing wrong?
Instructions:
Find the 4th up to and including the 7th letter of the string "wonderful day".


"wonderful day". substring(4, 8);


#2

@micromaster54101

you can check this answer which will help you .......I too had doubt in this part

As you have in the exmple, Lets take the word "Laptop" and find the 4th letter up to 6th(including).

So the word "laptop" has 6 letters.
Lets say we want to find the 3th letter.
To count a substring word, you start by 0 like:
L = 0
a = 1
p = 2
t = 3
o = 4
p = 5

So if you want to find the 4th letter up to the 6th (including) you start from 3 and end with 6 like:

"laptop".substring(3,6)
Remember, if you want to start counting from the 3th letter, you put (2,X)
if you want to count from the 6th letter, you put (5,X)

So if you take the word "wonderful day" and want to find the 4th letter up to the 7th(including) you say:

"wonderful day".substring(3,7)


#3

Thanks for your help. It seems for some reason, when I type substring, it doesn't register it and it doesn't turn blue, but when I pasted it, it recognized it.


#4

Thnx for taking my answer lol! :sweat_smile:


#5

you are welcomed :smile:


#6

I had the exact same problem. I actually reached the answer "wonderful day".substring(3,7) but the system did not recognise that until I pasted the answer from here. Seems like a bug in the system.

Also I don't think the lesson is clear enough about the mechanics of .substring. "Start chopping" and "finish chopping" have different (asymmetric) mechanics (?).

"Batman".substring(0,3) --> you start chopping at position 0 and 0 is INCLUDED in the cut. You finish chopping in position 3 and position 3 is EXCLUDED.

"laptop".substring(3,6) --> you start chopping at position 3 and it is INCLUDED in the cut. You finish chopping in position 6 (which doesn't exist) and position 6 is EXCLUDED.

Is this correct ?


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.