How do you find the number of characters in a String?
By using the length method:
int n = name.length();
How do you combine two Strings?
You can concatenate two Strings with the + operator.
For example:
String firstName = “Laurie”;
String lastName = “Stiles”;
String fullName = firstName + lastName;
How do you compare two strings?
Use .equals() method instead of ==
Wrapper Class
A wrapper class is class that is “wrapped around” a primitive data type and allows you to create objects instead of variables. However they are rarely used this way since wrapper classes are immutable. Plus, they are not as easy to use as variables for simple operations. They are more commonly used for the static methods provided for useful operations. One example is the Character wrapper class for the char data type. It provides numerous methods for testing and converting character data.
Character class methods for testing char values
* Where all methods accept a char value as an argument
How do you use the Character class methods on the contents of a String?
Write a loop and use them in conjunction with calling the charAt() method on the String.
For example:
for (int i = 0; i < str.length(); i++) { Character.toUpperCase(str.charAt(0)); }
Character methods for case conversion
String methods that search for a substring
String methods for getting a character’s or substring’s location
The indexOf and lastIndexOf methods can search for a character or a substring within a calling String. If the item is found, it’s position is returned. Otherwise -1 is returned.
* Where the “int start” parameter specifies the starting point in the String that you want to begins searching (using index notation).
String methods for extracting substrings
String methods that return a modified copy of a String object
Tokenizing
The process of breaking a String down into its components, which are called tokens. The String class’s split() method can be used to tokenize Strings. It accepts a delimiter as an argument which be expressed as a regular expression as well as a comma, space, hyphen, @ or even a word like “and”.
For example:
String str = “This is how you tokenize a string”;
String[] tokenArray = str.split(“ “);
for (String a : tokenArray)
System.out.println(a);
Output:
This
Autoboxing
Java’s process of automatically “boxing up” a value inside an object. This is rarely used but is convenient to leverage when you want to perform an operation on a primitive variable, but the operation can only be used with an object. For example, an ArrayList cannot hold primitive values; it’s intended for objects only. Therefore if you want to store integers in an ArrayList, you can use the wrapper class, Integer. Autoboxing will performed automatically whenever an integer is added so you don’t need to create an object first.
Unboxing
The opposite of autoboxing. It is the process of converting a wrapper class object to a primitive type. Continuing with the ArrayList of Integers example, unboxing will be automatically performed if an integer is removed from the list. That is it can stored as a primitive data type int automatically.
What methods does the StringBuilder class provide that the String class does not?
How do you remove a letter from a String?
Given that strings are immutable you cannot do this in a single method like you can with the delete() method of the StringBuilder class. However, you can work around this by splitting the string into substrings, one before the letter you want to remove and another after, and then joining them.
For example:
public String missingChar(String str, int n)
{
String front = str.substring(0, n);
/*/ Start this substring at n+1 to omit the char.* String back = str.substring(n+1, str.length());
return front + back;
}
How do you extract the last character in a String?
char lastLetter = str.charAt(str.length() - 1 );