Program Logic

Unit 4 Index

String Methods

Common String Methods

String manipulation is something that every developer does. For instance, if you need to determine if a password meets the minimum length standards, then you can use the .length() method on a String variable.

String.toLowerCase()

As you may have guessed, the .toLowerCase() method changes a string value to all lowercase letters. Doing so can be extremely handy when comparing end-user input with a String variable or literal in your code. This library method returns a lowercase version of your string. Typically, when something is returned to you, you need to be prepared to accept it.

To put this method into words you might say “my lowercase variable value will equal the lowercase version of my original string after its run through the .toLowerCase() library method.” Simplified Java code might be similar to this: answer = answer.toLowerCase();

String.toUpperCase()

The .toUpperCase() method changes a string value to all uppercase letters. Just like with the .toLowerCase() method, this can be extremely handy when comparing end-user input with a String variable or literal in your code. Simplified Java code might be similar to this: answer = answer.toUpperCase();

Unless the requirements say you must display the data in a particular way, it does not usually matter if you use .toUpperCase() or .toLowerCase(). The point is, you do not know what the end-user will enter, therefore you must manipulate their input so you can deal with it in code.

String.length()

As I stated earlier, if you need to determine if a password meets the minimum length standard, then you can use the .length() method on a String variable. Just like the previous two library methods, .length() returns something. However, in this case, it returns an integer value that represents the length of a string. Keep in mind, spaces are counted.

The Java code would look like this:

int myStringLength = myOriginalString.length();
Remember, spaces count, so the string “Java is fun” would return eleven as its length.

String.substring()

When you need to extract part of a string from the entire string, use the .substring() method. Be careful with this method. First, there are different ways to determine which part of the string you want to target. Second, this method tends to differ from one language to the next. Third, the first character in a string is at position zero. Position is often referred to as index.

The word “start” represents where in the string you wish to begin. Keep in mind, the first character is at index zero. If you only provide a start index, then the characters from the start index to the end of the string are returned.

The word “end” represents where in the string you wish to end...almost. In Java, the end index does NOT include the character at that end position. Whether the end index is inclusive or exclusive to the returned substring tends differs from language to language, so it is always a good idea to thoroughly test your substring code.

Let's get a better understanding using the string “Java is fun”. Please carefully review the below Java code snippet.

// Using .substring()
String fullString = "Java is fun";
String fun = "";
String java = "";

fun = fullString.substring(8);
java = fullString.substring(0,4);

System.out.println("Start at index 8 and go to the end of fullString results in: " + fun);
System.out.println("Start at index 0 and go to index 4 of fullString results in: " + java);

Try to match the above code with the below sample output.


String.contains()

The library method .contains() is a handy way to determine if your String variable contains a certain sequence of characters. This method returns true if the sequence was found, or false if it was not. Simplified Java code might be similar to this:

String fullString = "Java is fun";
boolean answer = fullString.contains("java");

Remember, Java is case sensitive, so after execution, the variable answer would be false. There is a difference between Java with an uppercase “J” and java with a lowercase “j” However, this is a great learning opportunity. We decided to see if the value of our variable contained the word “java” and in order to stack the deck in our favor, we will chain together two library methods. Simplified Java code might be similar to this:

String fullString = "Java is fun";
boolean answer = fullString.toLowerCase().contains("java");

First, change the contents of the variable to all lowercase letters. Second see if it contains our desired character sequence.

Comparing Strings

String comparison means you are comparing two strings to see if they match. Here is a scenario. You ask the end-user a question and they must answer either “yes” or “no”. Then you compare their answer with your variable or named constant. You write more code depending on whether they answered yes or no.

The thing is, in Java you cannot compare two strings by using the double equals sign ( == ), which is a comparison operator. In Java, you must compare two strings by using the .equals() method or better yet the .equalsIgnoreCase() method. Why? Because Java is case sensitive.

Typically, when you compare two pieces of data, you want to make a decision based on whether or not the values match each other. You have not yet studied decision structures, but I know you will be able to follow my code example. Please study the code below.


Scanner input = new Scanner(System.in);

String answer = "";
final String YES = "yes";

System.out.println("Do you need to schedule a meeting with me? Please answer yes or no.");

answer = input.next();

if(answer.equalsIgnoreCase(YES)){
	System.out.println("Great, I am always willing to meet");
}

The line of code that starts with the keyword if is the decision statement. Basically you are saying if what the end-user entered and my named constant compare equally (without considering their case), then print the statement between the curly braces. The last chapter in this unit goes over the Scanner object.

Do some critical thinking to understand why .equalsIgnoreCase() is better than just .equals(). Remember, Java is case sensitive.

What you Learned

What's next?

The next chapter in this unit is: Common Number Methods