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
How are any number of repetitions matched?
With *
What does [^a] match?
Anything but a
How is whitespace / non-whitespace matched?
\s matches any whitespace character; \S matches anything that isn’t whitespace
How is any letter matched?
[a-zA-Z]
How is anything buy x, y, or z matched?
[^xyz]
How can strings be replaced based on regex?
With string.replaceAll(pattern, substring)
Alternatively using .replaceFirst()
How is regex implemented behind the scenes?
The pattern is compiled into a Pattern object.
Together with the string that is to be matched, this pattern is used to generate a Matcher object that does the actual matching
How many bytes is a character?
Two
How are file paths represented and what behaviours do they have?
They are represented with Path. To create one from a string use the Paths.get(string) static method.
Path objects implement .getFileName(), .isAbsolute() and .toAbsolutePath()