Solution 1:

Synthesizing what Cesareo has done and the comments from mattos, the general idea is that since we know that

$$u(x,t) = f(x-2t),$$

then although $x \geq 0$ and $t \geq 0$ (I'm assuming this is the domain in which you are solving the equation, given your initial data), $f$ can still take negative inputs if $x < 2t$.

Now, we apply the initial data as follows. For $x \geq 0$, we have

$$u(x,0) = f(x - 2(0)) = f(x) = x$$

where the last equality is obtained by substituting the given initial data. Note that since this is only true for $x \geq 0$, this means that the function $f$ is basically $f(s) = s$ for $s \geq 0$.

For the other initial data, for $t \geq 0$, we have

$$u(0,t) = f(0 - 2t) = f(-2t) = t.$$

Note that for this case, we are dealing with $t > 0$ and thus the argument in $f$ is now negative. Rewriting this, we have that $f$ is basically $f(s) = -\frac{1}{2}s$ for $s \leq 0$.

Summarizing the above, we have

$$u(x,t) = f(x - 2t) = \begin{cases} x - 2t &\text{ for } x - 2t \geq 0 \\ -\frac{1}{2}(x-2t) &\text{ for } x - 2t \leq 0\end{cases}. $$

This is consistent with the formula from Cesareo when written with Heaviside step functions.

(Note that the formula is consistent at $x = 2t$ since both cases give $f(0) = 0$.)