In the most common contemporary graphical desktop interfaces, when you perform double-click in a text, the word around your cursor in the text will be selected.†
For example, let |
be your cursor in abc de|f ghi
.
Then, when you double click, the substring def
will be selected.
Input/Output
You receive two inputs:
- a string and
- an integer. Your task is to return the word‐substring of the string around the index specified by the integer.
Your cursor can be right before or right after the character in the string at the index specified. If you use right before, please specify in your answer.
Specifications
- The index is guaranteed to be inside a word, so no edge cases like
abc |def ghi
orabc def| ghi
. - The string will only contain printable ASCII characters (from U+0020 to U+007E).
- The word
word
is defined by the regex(?<!\w)\w+(?!\w)
, where\w
is defined by[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]
, or “alphanumeric characters in ASCII including underscore”. - The index can be either 1‑based or 0‑based. If you use are 0‑based index, please specify it in your answer.
Test cases
The cursor position visualization is for demonstration purpose only; it is not a required output.
The cursor is indicated right after the index specified.
Here, the sample
string indices are 1‑based.
sample | index | visualization | output |
---|---|---|---|
abc def |
2 |
ab|c def |
abc |
abc def |
5 |
abc d|ef |
def |
abc abc |
2 |
ab|c abc |
abc |
ab cd ef |
4 |
ab c|d ef |
cd |
ab cd |
6 |
ab c|d |
cd |
ab!cd |
1 |
a|b!cd |
ab |
†) This feature has more complicated properties, but they are not required to be implemented for this challenge.
we're
? \$\endgroup\$"ab...cd", 3
return? \$\endgroup\$