A common task in text editing is inserting one sequence of characters before
another one. For example, in the String "10 10 bills", we might want to insert a dollar sign ('$') before the second occurrence of the number "10". Thus, the original string would become "10 $10 bills". You are to write a
method which takes an original String and a series of insert instructions and
performs the instructions, in the order they are given. You should return the modified String.
Instructions will be of the form "#<insert># #<before># <index>", where
<insert> is the sequence of characters to be inserted, <before> is the sequence
of characters before which to insert <insert>, and <index> is the index of the
occurrence of <before> (starting from 1), before which to insert <insert>. In order
to facilitate spaces in <insert> and <before>, both sequences will be
surrounded by '#'. Thus the command "#insert this# #before this# 3" would
insert "insert this" before the third occurrence of "before this".
If, for some command, there are less than <index> occurrences of <before>, then do nothing. For example, in the String "$100
$100", there is no third occurrence of "$", so a command that inserts before
the third occurrence of "$" should be ignored.
|