1. Working with Strings
Let’s now find out more about Strings!
2. String recap
We already mentioned that `Strings` are not primitives but that they come with built-in functionality to manipulate them easily. Let's find out more about that!
3. String.length()
We quite often want to find out how long a `String` is. To get the number of characters in a `String`, we can use the `.length()` method, which returns an integer. For example, here, we find the length of the `String` `userName` and save it to the variable `userNameLength`.
4. Changing case
Now, let's change all letters to lowercase or uppercase. We can use the `.toLowerCase()` or the `.toUpperCase()` methods. For example, if we want to allow users to enter "YES" in any format, we can convert it using `.toLowerCase()` to "yes".
5. String concatenation
One other useful feature of `Strings` is that we can combine them using the + symbol. This is called `String` concatenation. For example, we can combine `Java is` with `awesome` using a plus and a space to have readable text.
6. Concatenation with numbers
Concatenation also works with numbers. Java automatically converts numbers into Strings, as in the example here.
7. Declare now, assign later
We can also create variables without giving them a value. We can first define the variable by stating its type and name, and then assign a value later.
This can be useful when dealing with large amounts of text or when we don't yet know the value of a variable.
For example, we can declare `courseCreators` first by writing `String courseCreators` semicolon, and then assign this long text to it later in the program.
This flexibility allows us to structure our code effectively.
8. Recap
That was a lot of information, so here is a recap to help you.
9. Let's practice!
Now, let's practice working with `Strings` in the next exercises!