This is the continuation of the previous part of Methods of the String Class (Part 1) . This the part 2 of that tutorial. Hope you enjoyed it and let’s get started.
The substring method
- returns new strings that are sub strings (parts) of the referred string (original String) object.
- this method requires two parameters.
- the end index is not a must. If not stated, the end will the end of index of the original string.
- as you can see even the space has given the index. (2nd sub string)
- let’s further explore this method. What will happen if you give the end index, a higher values than the length of the original string ([length – 1] to be exact) or what will happen if you give a negative value?
- as you can see, Java is too smart for us. Java has identified it as an exception.
- giving an negative value will also end up with the same exception. (the exceptions can be handled using try-catch blocks which will be discussed later).
The trim method
- returns the whole original string after removing the leading and trailing white spaces.
- check the difference between original text and resulting text.
The toLowerCase Method and toUpperCase Method
- returns the whole string in lower case letters and upper case letters respectively.
The equalsIgnoreCase Method
- returns a boolean after comparing the original text with the text given in the parameter.
This is it for the methods of the string class but I will come back with a another set of methods which feat the java string buffer class.
So until that Happy Hacking.