03a Character and String Processing
03a Character and String Processing
03a Character and String Processing
Processing
CSX3002/ITX2001 Object-Oriented Concepts and Programming
CS4402 Selected Topic in Object-Oriented Concepts
IT2371 Object-Oriented Programming
Character Fundamentals (1)
2
Character Fundamentals (2)
3
Character-Handling Library
4
Character Handling Methods (1)
Character-related methods
static boolean isDigit(char ch)
static boolean isLetter(char ch)
static boolean isLetterOrDigit(char ch)
static boolean isLowerCase(char ch)
static boolean isUpperCase(char ch)
static boolean isWhitespace(char ch)
static char toLowerCase(char ch)
static char toUpperCase(char ch)
and more…
5
Character-Handling Methods (2)
Upcoming example
◦ isLowerCase
Returns true if lowercase letter (a-z)
◦ isUpperCase
Returns true if uppercase letter (A-Z)
◦ toLowerCase
If passed uppercase letter, returns lowercase letter
A to a
Otherwise, returns original argument
◦ toUpperCase
As above, but turns lowercase letter to uppercase
a to A
6
Example 1
CharacterProcessing1.java
According to Character.isDigit:
8 is a digit
# is not a digit
According to Character.isLetter:
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to
Character.isLetterOrDigit:
A is a letter or a digit
8 is a letter or a digit
# is not a letter or a digit
Fundamental of Strings in Java (1)
String
Collection of characters
Can include anything that can be a character
Letters
Digits
Special symbols
Method Description
length() Returns the length of this string
charAt(int index) Returns the char value at the
specified index.
isEmpty() Returns true if length() = 0
String Manipulating Methods
Method Description
concat(String s) Concatenates the specified string to the
end of this string.
startsWith(String prefix) Tests if this string starts with the specified
prefix.
endsWith(String suffix) Tests if this string ends with the specified
suffix.
contains(CharSeq seq) Returns true if and only if this string contains
the specified sequence of char values.
substring(int beginIndex) Returns a new string that is a substring of this
string.
substring(int beginIndex, Returns a new string that is a substring of this
int endIndex) string.
String Manipulating Methods
Method Description
replace(char oldChar, Returns a new string resulting from
char newChar) replacing all occurrences of oldChar in this
string with newChar.
trim() Returns a copy of the string, with leading
and trailing whitespace omitted.
String Comparison Methods
Method Description
compareTo(String str) Zero - equal lexicographically
Positive – greater than the parameter
Negative – less than the parameter
compareToIgnoreCase(String str) Similar to compareTo() but the cases
of all characters are ignored.
== Checks if the two strings are the same
object.
equals(Object anObject) Checks if the contents are the same.
StringProcessing.java
split()
Method Description
public String[] split(String regex) splits the string around matches of the
given regular expression.
public String[] split(String regex, int limit) splits the string around matches of the
given regular expression. Limit is used for
controlling the number of times the
pattern is applied and affecting the
length of the resulting array
Example
StringSplittingExample.java