-
Title:
strings - Intro to Java Programming
-
Description:
-
Here are the answers that I got. The length method returns the number of
-
individual characters in the string. There were 7. The substring method
-
extracts a substring that sits inside the bigger string. In our case, it
-
extracted this string. Whenever you want to extract substrings, you have to
-
understand how the positions in a string are numbered. In Java the initial
-
position is 0, that may sound strange but it's actually pretty useful, and then
-
it goes from there. The string has length 7 and the position's in it are 0, 1,
-
2, 3, 4, 5, 6. That's 7 position's. Now in the substring here you see this 3
-
here. That is the position of the first character that we want to include in
-
the substring. The seven here is the first position that we don't want to have
-
anymore. We don't want position seven. In fact, there is no position seven. We
-
want the ones from three to six. That sounds a bit odd to most people when they
-
see it the first time. But there is an advantage. When you subtract these two
-
numbers, 7 minus 3, that's 4, and that's the length of the substring that
-
you're extracting. The next method here, the indexOf method finds the position
-
of a given character. So, over here I want to know where does the C occur for
-
the first time. And it will go through and say, that's at position three. And
-
finally, you already seen this plus operator in lesson two. It takes two
-
strings and glues them together or as we like to say in programming is, it
-
concatenates them. So, here is the concatenation of Hello and Udacity, notice
-
there's no space in between because there was no space in either this or that
-
string. If you wanted a space, you'd have to add a space. Strings are really
-
useful when you work with text. Sarah has a couple of nifty exercises prepared
-
for you when you get to work with a rather long text and take it apart. And put
-
pieces back together by using some of the string methods that you've seen here.
-
Enjoy.