I also have trouble understanding what you are trying to accomplish. In any case, assuming that
(note that some of the spaces are repeated multiple times) your code produces the output:
:This, is , a , test.This","is","a","
test.
To me, it looks like the code does exactly what I would expect it to do, i.e. splits the original string into a string array on commas, strips away the leading and trailing spaces of each array element (first for loop), appends "," to each element array but the last (second for loop), and finally tacks all element arrays onto the original string (third for loop).
Assuming you want instead to produce "This","is","a","test." simply do: