What Is A String?: Tutorial 4
What Is A String?: Tutorial 4
Tutorial 4
Java Strings
What is a String?
In java and unlike C++, the string data type is now an object type. Notice that the class name is starts with an uppercase. If you mess this up, Java will not know what you are referring to. By it's conception, a string is anything and everything grouped together between 2 double quotes. For example, "Hello world" is one string while "sdad anesd ccn " is another.
Using Strings
Strings can be manipulated in numerous ways. The first is with some member functions of the string class. Let's see an example first before we continue.
www.cstutoringcenter.com
//5. print out the beginning character using charAt() System.out.println("first character: " + word.charAt(0)); //6. make the string shorter word = word.substring(0, 4); System.out.println("shorter string: " + word); } }
When you run the above program, here is the output that you will get:
Length: 9 toUpperCase: ALEXANDER toLowerCase: alexander trim: Alexander indexOf('s'): -1 first character: A shorter string: Alex
A lot certainly has happened in this program. Let's observe some of the most used functions in the String class.
int length()
The length function will simply return the length of a string as an integer.
String toUpperCase()
The toUpperCase function will return the uppercase version of a string. Say you have a string "Welcome". This function will return "WELCOME".
String toLowerCase()
The toLowerCase function will return the lowercase version of a string. Say you have a string "Welcome TO Earth". This function will return "welcome to earth".
String trim()
The trim function will return the string without leading or trailing white space characters. Say that a string was " hello ". There are 4 spaces in the front and 3 spaces at the end. The trim function would make this "hello".
www.cstutoringcenter.com
indexOf(int ch) indexOf(int ch, int begin) indexOf(String ch) indexOf(String ch, int begin)
Notice that there are 4 different functions listed here. All of them perform the same overall action of returning an integer, which represents the FIRST OCCURRANCE of a character or String contained in that string. So say that we have the string "Hello" and we say indexOf('l'). This function will use the first function above and return 2. Notice it doesn't return 3. We can also say, using the same string "Hello", indexOf("He"). It will return 0 as the string that you are searching for starts at index 0. By default, if a string or character is not found in the string, the any of the functions will return -1.
String Equalities
With Java, there are numerous methods they provide that check for two strings being equal. To show this, here is a small program that will check for a certain string as a command line argument.
www.cstutoringcenter.com
private static String word = null; private static String keyword = "HELLO"; public static void main(String args[]){ //standard error check: if(args.length < 1){ System.out.println("Argument requires one word."); System.exit(1); } //retrieve the sentence: word = args[0]; //check for the keyword using different equals methods: if(word.equals(keyword)){ System.out.println("equals true: " + word); } if(word.equalsIgnoreCase(keyword)){ System.out.println("ignoreCase true: " + word); } //check for the keyword using different compareTo methods: if(word.compareTo(keyword) != -1){ System.out.println("found using compareTo: " + word.compareTo(keyword)); } if(word.compareToIgnoreCase(keyword) != -1){ System.out.println("found using compareToIgnoreCase: " + word.compareToIgnoreCase(keyword)); } } }
This program will accept as a command-line argument, one word which we will call our "keyword". It will then perform 4 checks using different member functions of the String class. Here is the output when you run the program with the argument "hello" SPELT EXACTLY AS IS (all lowercase).
ignoreCase true: hello found using compareTo: 32 found using compareToIgnoreCase: 0
Let's discover why. Here are the descriptions of the member functions:
www.cstutoringcenter.com
you called the member function from. So in the above program, the initial string is the variable word. This method returns a boolean value. If true, the string parameter is an exact copy of the initial string. If false, the argument is not a copy as something differs in the string. As noted, the above program returns false when the equals() method is used because of the case difference.
www.cstutoringcenter.com