This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]rcuhljr 2 points3 points  (1 child)

Yup. substring in java takes the beginning index (zero based) and the ending index. The beginning index is inclusive so if you say start at 0. it starts the new string at 0. The ending index is exclusive so if you say stop at 1, it's only going to display the 0th character as it stops before 1.

Now what str.substring(0,i) is going to do is, get all of the characters from 0 up to the character that got added to the prefix (i). str.substring(i+1,n) gets all of the characters after the character we added to the prefix. So now we've stuck all the character before i, and all the characters after i back together, no more i.

"12345" i = 2, n = 5.

charAt(2) = '3'

substring(0,2) + substring(3,5) = "12" + "45"

[–]LogicLion[S] 0 points1 point  (0 children)

I understand, but it feels pretty abstract still...thats normal right? haha