For this kind of questions, a quick search on stackoverflow is usually a great source of solutions. For example: