The String and Integer Classes are very useful to know. Some of their methods (runnable pieces of code) are very useful to everyday coding.
The String Class
substring(start, end) – substring can be use to grab a certain section of a string. It is used by giving it a start and end position. If no end position is given it assumes the end of the string. Also important to note, the end position is actually one more than what is grabbed. You can just use the substring right away or you can save it either over the original or in its own variable. You can see in this example:
length()
length() is very useful method. It allows the user to use the length of a String as a variable in integer form. For example using our variables above:
Which outputs:
This is very useful in loops and if statements. That is, if you want to run certain code if the String is a certain length or if you want to loop through all the characters of a String of unknown length, unknown at least to the programmer.
equals(String)
The equals method is very useful if you want to check if two Strings are identical. Note that “==” will not work for Strings. This returns a boolean answer which is typically used for if statements or loops.
Given this code:
What do you think it will output?
The answer is:
This is because the equals() method cares about capitalization. To see if they are have all the same characters in the same order regardless of capitals then you have to use this next method.
equalsIgnoreCase(String)
This is the exact same as the equals() method except that, as it says, it ignores the case. If we were to have used equalsIgnoreCase() instead of equals() in the above example we would have received an output of:
compareTo(String) and compareToIgnoreCase(String)
These check if a String is greater than, less than or equal to another String lexicographically speaking. This means that it checks the String, reading from left to right and compares each character against the alphabet. This then returns a number. If the number is 0 they are identical. If it is greater than 0 it is further down the alphabet. If it is less than 0 it comes earlier in the alphabet.
Note that when using compareTo() you are using the first String as the base location and the returned number is telling you where the second string is relative to the first.
We would use it again mostly in conditional statements, like in loops or if statements. For example:
Which outputs:
charAt(int)
This returns the character (char) at a certain position (like always, starting with 0). For example:
Which outputs:
split(String)
The split method splits a String into parts and stores it in an array. In the brackets you put a regular expression. This denotes what it is being split by. Some of these expressions are:
indexOf(char) and indexOf(char,int)
indexOf(char) outputs the first occurrence of that character in the String. indexOf(char,int) outputs the first occurrence of that character in the String after the provided index. This looks like:
Which outputs:
lastIndexOf(char) and lastIndexOf(char,int)
These work the exact same as indexOf() except that it checks backwards through the String and if specified uses the index as a starting place then goes back from there.
isEmpty()
This returns true if the length of the String is 0.
toLowerCase()
This changes all characters in a String to lowercase. For example:
Which would output:
toUpperCase()
This changes all characters in a String to uppercase. For example:
Which would output:
The Integer Class
The Integer Class is interesting, because, I find at least, that it is less used than the simpler int. It is perfectly acceptable to write both of these pieces of code:
The only difference is that since the second one is an Integer object and not a variable a method must be used to get at the value stored in it.
intValue()
Probably what you would use most to get the value if using the Integer object. using the above variables it would look like this:
It is clear that while x1 can just be called upon, x2 requires the use of a method.
Using a similar call the number stored in the object can be converted and saved into multiple different number types these methods include:
doubleValue()
floatValue()
shortValue()
longValue()
These are all used in the same way that intValue() is.
parseInt(String)
In my opinion the most useful Integer method. This method allows you to convert a String, if it is convertible, to an Integer.
As you can see the number 56 was stored as a String which can not be used as a number. But once you parse it, it can be.
Summary
Both the String and Integer Classes are very useful to know to help you get better at coding. The Integer object, while less used than the more simple int, is very useful for converting information to other types whether it be String to int or int to double, float, long or short. The String Class is also very useful for finding characters, extracting sections of code or checking alpha-numerically whether one String comes before another.