Almost every language has a built-in function that can split a string at a given position. However, as soon as you have html tags in the string, the built-in function will not work properly.
Your task is to write a program or function which splits a string at the nth character but does not count characters of html tags and will output a valid html. The program must keep the formatting. Spaces outside the html tags may be counted or not counted, as you wish, but must be preserved. You can, however, exchange multiple consecutive spaces into a single space.
Input:
- the string
- the position to split at (0-based)
These can be taken as program or function arguments or can be read from the standard input.
Output: The split string which can be returned or written to the standard output.
The input will be valid html, it won't contain any entities (such as
). Tags that are opened after the character limit should be omitted from the output (see the last example).
Example:
Input: <i>test</i>
, 3
Output: <i>tes</i>
Input: <strong><i>more</i> <span style="color: red">complicated</span></strong>
, 7
Output: <strong><i>more</i> <span style="color: red">co</span></strong>
Input: no html
, 2
Output: no
Input: <b>no</b> <i>html root</i>
, 5
Output: <b>no</b> <i>ht</i>
Input: <b>no img</b><img src="test.png" />more text
, 6
Output: <b>no img</b>
You can use any language and the standard library of the given language. This is code golf, shortest program wins. Have fun!
<
and>
instead of<>
, so no (<
or>
won't be present either). \$\endgroup\$<i>ab</i><b>cd</b> 1
? \$\endgroup\$<i>a</i>
? \$\endgroup\$<i>a</i><b></b>
(Which makes sense if you consider thatb
could also bediv
orimg
.) \$\endgroup\$