Strings are special

In Java, a String is much more than a typical data type, it is also an object. This is because String has methods that help it perform tasks. A very commonly used String method is the equals() method, which is used to determine if two Strings are the same. In this chapter, we will explore some of the different types of methods that the String object has to offer.

 

toUpperCase()

This method changes all the letters in a String into lower case letters.

String myString = “Daamn Daniel!”;
String myString2 = myString.toUpperCase();
System.out.print(myString2);

The value of the new string will be DAAMN DANIEL!

The toLowerCase() method will create the reverse effect.

 

charAt()

This String method will determine the character that is at a specified position in a String.

String myString = “I can’t feel my face”;
char myCharacter = myString.charAt(8);
System.out.print(myCharacter);

strings
This will produce the character ‘f’ since f is the 8th character.

the index position counting starts at 0 not 1 so myString.charAt(0) is ‘I’

 

replace()

This method searches a String for a specified text then it replaces it with a different text.

String myString = “1.4 million U.S. citizens serve in the U.S. military”;
myString = myString.replace(“U.S.”,”United States”);
System.out.print(myString);

Every occurrence of ‘U.S.’ in the String is replaced with ‘United States’ so the output will be ‘1.4 million United States citizens serve in the United States military’

 

substring()

This String method will extract the characters from a string, between two specified indices

String myString = “I can’t feel my face”;
String myString2 = myString.substring(1,5);
System.out.print(myString2);

strings
This will output ‘can’ because the text in-between index 1 and 5 is can.

If we give the method one number instead of two, ie(myString.substring(7);) the method will output all the characters after the specified number. So the output for myString.substring(7); is ‘feel my face’

 

split()

Split will split a string into an array of substrings.

String names = “Michelle,Gonzales,Kwesi,Sundeep,Drake”;
String [] nameArray = names.split(“,”);
System.out.print(nameArray[2]);

The split method will convert the names string into an array of strings separated by a comma

“Michelle,Gonzales,Kwesi,Sundeep,Drake” 0.       Michelle
1.       Gonzales
2.       Kwesi
3.       Sundeep
4.       Drake

So nameArray[2] is “Kwesi”

 

Program: use the toUpperCase() method and the equals() method to determine if two names are the same. Ex ‘John Doe’ is the same name as ‘john DOE’ but ‘John Doe’ is not the same as ‘Jon Dough’.

 

Program: Display only the first 10 characters of a sentence followed by ‘…’ Ex ‘progress makes us happy’ will become ‘progress m…’

 

These are just a few of the methods that the Java String object has. To view the full list; go to https://docs.oracle.com/javase/7/docs/api/java/lang/String.html

Back to Course Menu
What do you think about this topic? Share your suggestions / comments