Java Reference
In-Depth Information
the
java.util.regex.Pattern
class for complete documentation [5,6]. In
its simplest form, searching for a regular expression consisting of a single char-
acter finds a match of that character. For example, the character 'x' is a match for
the regular expression “x”.
The
split()
method takes a parameter giving the regular expression to use
as a delimiter and returns a
String
array containing the tokens so delimited.
Using
split()
, the first example above becomes
String str ="This is a string object";
String[] words = str.split ("");
for (int i=0; i < words.length; i++)
System.out.println (words[i]);
To use
“*
„
as a delimiter, simply specify
“*
„
as the regular expression:
String str ="A*bunch*of*stars";
String[] starwords = str.split ("*");
For most string-splitting tasks, the
String.split()
method is much eas-
ier and more natural to use than the
StringTokenizer
class. However,
StringTokenizer
is still useful for some tasks. For example, an overloaded
StringTokenizer
constructor allows you to specify that the tokens to be
returned include the delimiter characters themselves.
10.12
Calendar
,
Date
, and
Time
Java offers several classes for dealing with dates and times. Some are in the
java.util
package:
GregorianCalendar
(a subclass of the abstract
Calendar
class)
Date
These two are in
java.text
:
DateFormat
and its subclass
SimpleDateFormat
The
GregorianCalendar
class can be used to represent a specific date accord-
ing to the Gregorian calendar (and the Julian calendar before that). Methods are
provided to compare calendar objects such as whether one date came before or
after another. The
Calendar
base class, which is abstract, holds static methods
that give information such as the current date and time:
Calendar this
-
moment
=
Calendar.getInstance ();
The hierarchy implies that there could be other calendar subclasses, e.g. Chinese,
but none has appeared in the core language as of version 5.0.
Search WWH ::
Custom Search