Strings and IO Flashcards
How can ranges be extracted from a string?
using string.subString(start, end).
Note that the second parameter is the index of the character AFTER the last character.
What is the result of “abcd”.subString(1, 3)?
“bc” - the second index is of the character AFTER
How are strings compared?
Using string.compareTo(other).
This returns 0 if they are the same, negative value if the parameter precedes the string alphabetically, and a positive value if the parameter comes after the string in the alphabet.
How are substrings found in a string?
With string.indexOf(substring)
This returns the index of the first occurrence. A second parameter can be used to start the search from a particular character index
How can regular expressions be matched to a whole string?
use string.matches(pattern) to check the format of a whole string OR test if it contains a particular substring
What is the basic regex syntax?
^ is the beginning of a string
[a-z] is a lowercase character from a to z
{3,4} isi the previous character pattern occurring 0 to 9 times
\d is the digit 0 to 9
$ is end of string
How are backspaces used in Java strings?
They are escaped with a another backslash i.e. “\” = \
How are groups represented in regex?
With brackets i.e. ([A-z][0-9]){3}
What does a full stop represent in regex?
Any character.
Hence use . to match a literal period
How is any digit matched?
with .
How are 0 or 1 repetitions matched?
With ?
How are 1 or more repetitions matched?
With +
What does a ? represent?
The previous character / pattern may occur 0 or 1 time
What does a + represent?
The previous character / pattern may occur one of more times
What does a * represent?
The previous character / pattern may occur any number of times