My first suggestion is that you should use Regular expression for this task, your method isn´t wrong but needs lots of code.