String Cheat Sheet
by, 10-18-2012 at 12:05 AM (3759 Views)
A string is a piece of text, that is zero or more characters in length. In Java, strings are considered to be pre-built objects. The variable for a string does not store the value of the string, rather it is a reference point, or pointer, to the storage location of where the string is kept.
Declaring a String:
Assigning a Value On Declaration:Java Code:String s1;
Assigning a Value With AssignmentJava Code:String s1 = "Hello World!";
Assigning a Value With InputJava Code:String s1; s1 = "Hello World!";
s1 = keyboard.next();
Common String Methods
The string class is 'immutable', which means that none of the different methods that strings use can change what is actually stored within the string itself. When methods alter a string, rather then saving it in the same storage location, it chooses a new storage location, and leaves the original unchanged. Any method that seems to change a string's contents is actually producing a modified version in an independent storage.
To identify how many characters reside in a particular strings, use the following method.
This will store the number of characters that are stored in the string s1 and store that number in the integer len.Java Code:int len; String s1; len = s1.length();
You are able to extract single characters from within a string, and you can even choose which character to extract; based upon the index number of the string.
This extracts the character with the index number ix, remember that the index number starts at 0 for the first character. The character would be saved in the variable extr.Java Code:char extr; String s1; int ix; extr = s1.charAt(ix);
Comparing ContentsYou can compare strings in couple of different ways.
The way that you will most often use is to compare the contents of the string. You are able to do that using the following method.
The other way is to compare the addresses that are stored in the variable itself. This is a lot less common, and mainly used in the mistaken idea that it will compare the contents of the string. To compare storage locations of strings use the following method.Java Code:String s1, s2; if( s1.equals( s2 ) ) System.out.println("Same string contents");
Another way that you can compare strings is by their alphabetic order. This type or comparison sorts the strings from A-Z. This is done utilizing the following method.Java Code:String s1, s2; if( s1 == s2 ) System.out.println("Same Addresses");
Java Code:String s1, s2; int compare; compare = s1.compareTo( s2 ); if( compare < 0 ) System.out.println("s1 is less than s2"); else if( compare > 0 ) System.out.println("s2 is less than s1"); else //compares for s1 == s2 System.out.println("s1 is the same as s2");
Another thing that you are able to do with strings is to convert their case. You can covert all the characters to uppercase, or convert all the characters to lowercase. This is done using the following method.
This does not change the string that is stored at the memory location s1; rather it makes two new strings and creates two new storage locations. An independent copy that is all lowercase is stored at memory location s3, while an independent copy that is all uppercase is stored at memory location s2.Java Code:String s1, s2, s3; s2 = s1.toUpperCase(); s3 = s1.toLowerCase();
Changing a String
You can add to a String using the '+=' format.
You can also replace the entire String using the '=' format.Java Code:String s1 = "Hello"; s1 += " World";
Java Code:String s1 = "Hello"; s1 = "Hello World";