As you probably have guessed a string is another data type that is used in programming,other data types for example are, integers and floating point unit, but as we have already learned, those two data types are used to represent numbers. Strings on the other hand are used to represent text such as words. It is comprised of a set of characters but it also can contain spaces and numbers. You should also make sure that your string is also always enclosed in quotation marks.
There is a multitude of different methods we can use to manipulate the string. Here are a few of them:
char charAt(int index)
String toUpperCase()
int compareTo(String string)
int indexOf(int ch)
String substring(int beginIndex)
boolean equals(Object obj)
String.length()
Make sure that before using these methods you initialize you string, if you forget to do this do not worry because the computer will defiantly remind you, in the form of an error message!
Here we have an example of a string and a string method. We can see that comment are being used to help explain the code. First we initialize the string, then we create a variable that is equal to the length of the string by using the str.length()
method, and finally we print of the length of the string. Can you guess what the length of the string is? Well if you guess 15 then you are correct!