
HI! Welcome to the 2nd tutorial of the String operations. In this lesson we are going to learn some quick skills needed for you to write a standard java program.
These are really important and please DO NOT SKIP this lesson. If you do it then you won’t be able to understand most of the things in the upcoming lessons. So stay focused to the subject 🙂
Now you can of course make a variable of type String.
1 |
String name; |
See the S in String is capital. You should always start String with a capital S. You can assign almost anything within “ ” marks, in to a String.
1 2 3 |
name = "Hi"; name = "10021323120.2345902"; name = "#$q23l;&%$#@@"; |
You can use the variable to get an output.
Eg:
1 2 3 |
String name; name = "bill gates"; System.out.println(name); |
OUTPUT:
bill gates
Remember!
Always remember you have to declare and assign a value to the variable before calling the print statement because, the computer runs each statement from top to bottom.
If you don’t do that, it will try to print a value that doesn’t exist at the moment, which is, of course, an ERROR!!
That means, if you try to do;
1 |
System.out.println(name); |
without writing
1 2 |
String name; name = "bill gates"; |
first, you will get an error.
Or if you try
1 2 |
String name; System.out.println(name); |
without writing
1 |
name = "bill gates"; |
in between the two statements, then, an error will occur…
OK. Now I have to tell you another cool fact. You can play with Strings using special methods. Just do this;
1 2 3 4 5 |
String name; String upperCaseName; name = "bill gates"; upperCaseName = name.toUpperCase(); System.out.println(uppercaseName); |
OUTPUT:
BILL GATES
What have we done here?? Assigning “bill gates” to the variable name is ok… but in the next statement, we see a new feature. name.toUpperCase().
This is an order that we do to the String variable name. We want all words in the name variable to be capital.
For that, we use a String feature called .toUpperCase() (Note the .dot before “t”).
This feature is called a “method”. We will do about methods extensively in the course but for now, get an idea of it by the following diagram…
A method is something like this;
Method is a kind of magic box or a black box where we put something to it and get something useful. In the above example,
The output “BILL GATES” given out from the method is assigned to a new String variable called upperCaseName.
Actually you don’t need to define a new variable for that. You may directly put that method to the basic output function.
1 |
System.out.println(name.toUpperCase()); |
Now here’s another special point:
We can assign the String output to the same variable.
1 |
name = name.toUpperCase(); |
If you feel this a little complicated, never mind 🙂 We’ll certainly do this in detail later.
The right hand side, that is name.toUpperCase(); is going to operate first.
That is, the black box will operate first and give the output string “BILL GATES”.
Step 1:
1 |
name.toUpperCase() |
Then what happens is that output will be assigned to the name variable in the left hand side.
Step 2:
1 |
name = “BILL GATES” |
1 2 3 |
name = "bill gates"; name = name.toUpperCase(); System.out.println(name); |
OUTPUT:
BILL GATES
Alright. Now there are many methods for you to experiment. Once you put a dot mark after the name, you may see how many methods are available for you to do some cool stuff…
I’ll give some of them here…
- toLowerCase() – can you predict what will happen?? Yeh it will change any capital letter in the String to a simple letter and finally you will get all simple String.
- length() – will return the length of the variable.
Note that this method will return an integer. NOT a String. OK?
1 2 3 |
name = "bill gates"; int len = name.length(); System.out.println(len); |
- indexOf(“at”) – returns an integer of the index of the character at the occurrence of the substring “at” in varible of “bill gates”.
In the example, the index is the number relevant to each letter of the word as follows.
b–0 , i – 1 , l – 2 , l – 3, (space) -4, g – 5, a – 6, t – 7, e – 8, s – 9
Note that the indexing is starting from 0, NOT 1.
That’s the usual form and, guys, you MUST remember that.
If the substring is not available in the name, -1 is returned.
1 2 3 |
name = "bill gates"; System.out.println(name.indexOf("at")); System.out.println(name.indexOf("q")); |
OUTPUT:
6
-1
- equals(name2) – This method will check whether String of name equals the String of name2 and returns a boolean value, that is, either true or false
1 2 3 4 |
name = "bill gates"; name2= "gates"; boolean isEqual=name2.equals(name); System.out.println(isEqual); |
OUTPUT:
false
obvious 🙂 Isn’t it??
- equalsIgnoreCase(name2) – From the name itself you can get the idea of what this method does. It just ignores the capital simple difference between two variables and check whether the two Strings are equal.
1 2 3 4 |
name = "bill gates"; name2= "BILL GATES"; boolean isEqual=name2.equalsIgnoreCase(name); System.out.println(isEqual); |
OUTPUT:
true
- charAt(n) – returns the character at n th index.
1 2 |
name = "bill gates"; System.out.println(name.charAt(5)); |
OUTPUT:
g
If the index is out of range, error will occur and the program will crash 🙁
Eg:
1 2 |
name = "bill gates"; System.out.println(name.charAt(500)); |
OUTPUT:
Exception in thread “main” java.lang.StringIndexOutOfBoundsException:
String index out of range: 500
OK guz. That’s all for the Strings (for now ;). Please experiment with those methods and get familiarize with them!!
In the next tutorial, we are going to review some math. But they are NOT hard at all! Let’s meet in the next tutorial. Bye guys!!