Extract the Prefix of a Filename
Problem statement
For this practice exercise, filename has the form prefix_suffix. Return exactly prefix, the nonempty text before the first underscore.
The prefix contains only ASCII letters, digits, dots and hyphens. The nonempty suffix may contain those characters and additional underscores. Both parts are opaque text: preserve the prefix exactly, including its case, dots, hyphens and leading zeros. No file extension is required.
Interview context
The source describes extremely simple basic filename processing in the first technical screen, but gives no operation or filename format. This first-underscore transformation, grammar, returned string and bounds are disclosed practice choices.
Function
filenamePrefix(filename: String) → StringExamples
Example 1
filename = "Run-7_part_2.log"return = "Run-7"The first underscore ends the prefix; the later underscore is inside the suffix.
Example 2
filename = "007_data.csv"return = "007"The prefix is opaque text, so its leading zeros are preserved.
Example 3
filename = "Report.v2_payload.txt"return = "Report.v2"Dots and uppercase letters in the prefix are preserved without extension parsing.
Constraints
- For this exercise, assume
3 <= filename.length <= 10000. - The prefix has at least one character from
A-Z,a-z,0-9,.or-. - The first underscore separates the prefix from a suffix with at least one character from the same set or another underscore.
- All inputs satisfy this grammar. There are no path separators or whitespace, and malformed input handling is outside this exercise.