Strings & Text Processing Flashcards
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
- boolean isDigit
- boolean isLetter
- boolean isLetterOrDigit
- boolean isLowerCase
- boolean isUpperCase
- boolean isSpaceChar
- boolean isWhiteSpace
* 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
- toLowerCase(char ch)
- toUpperCase(char ch)
String methods that search for a substring
- boolean startsWith(String str) - returns true if the calling String begins with the substring passed into str
- boolean endWith(String str) - returns true if the calling String ends with the substring passed into str
- boolean regionMatches(int start, String str, int start2, int n) - determines if specified region of two strings match. The first parameter is the starting location (using index notation) of the region in the calling String; the second parameter is the other String used in the comparison; the third parameter is the starting location (using index notation) of the region in the second String; and the final parameter is the length of the region you want compared in both Strings.
- boolean regionMatches(Boolean ignoreCase, int start, String str, int start2, int n) - an overloaded version of the above method that has an additional parameter ignoreCase to which you can pass “true” or “false”
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.
- int indexOf(char ch)
- int indexOf(char ch, int start)
- int indexOf(String str)
- int indexOf(String str, int start)
- int lastIndexOf(char ch)
- int lastIndexOf(char ch, int start)
- int lastIndexOf(String str)
- int lastIndexOf(String str, int start)
* 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 substring(int start) - returns a copy of the substring that begins at the position specified in the start parameter and goes until the end of the calling object’s String
- String substring(int start, int end) - returns a copy of the substring within the range specified via the parameters. However, note that the character at the starting position is included whereas the one at the end is not!
- void getChars(int start, int end, char[] array, int arrayStart) -
- char[] toCharArray() - returns all of the characters in the calling object as a char array
String methods that return a modified copy of a String object
- String concat(String str) - returns a copy of the calling String object with the contents of str concatenated to it
-
String replace(char oldChar, char newChar) - returns a copy of the calling String with any old characters replaced with the new ones. Note that this method will only work with user a specified char or Character Sequence (i.e. “y”, “z” or “yyyy”, “zzzz”). It does work with arguments such as str.replace(charAt(n), “”); which I tried to remove a character at a user specified index position. The following code also doesn’t work:
char firstLetter = str.charAt(0);
char lastLetter = str.charAt(str.length()-1);
str.replace(firstLetter, lastLetter);
str.replace(lastLetter, firstLetter);
return str; - String trim() - returns a copy of the calling String with all leading and trailing white space characters removed
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
- is*
- how*
- you*
- tokenize*
- a*
- string*
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?
- append()- this overloaded method accepts any primitive data type, a char array, or a String object. They append a String representation of their argument to the calling object’s current contents.
- insert(int position, value) - this overloaded method accepts an integer to indicate the position in the calling object’s String where the insertion should begin and the value to be inserted. The value can be any primitive data type, a char array, or a String object.
- replace(int start, int end, String str) - while the String class also has a replace method, this one differs in that it replaces a substring as opposed to occurrences of a character.
- delete(int start, int end)
- deleteCharAt(int position)
- setCharAt(int position, char ch)