You've already taken the first step by admitting you have this problem -- and not only admitting it but also realizing it's wrong and wanting to do something about it.
Remember, God is just as concerned about our thoughts as He is about our actions, and He doesn't want evil thoughts to occupy our minds. One reason God destroyed the world in Noah's time was because "every inclination of the thoughts of his (man's) heart was only evil all the time" (Genesis 6:5). Evil thoughts lead to evil deeds -- and even when they don't, they focus our hearts on evil rather than good.
How can you conquer bad thoughts, whatever form they take? Begin by turning to God and submitting your whole life -- including your mind -- to Jesus Christ. If you have never done so, confess your sins to Him today, and by faith ask Him to come into your heart.
Then fill your mind with God's truth. In other words, don't just empty your mind of evil thoughts, but fill it with good thoughts -- in other words, with God's truth as it is found in the Bible. Make the Bible part of your life every day, and memorize as much of it as you can. Then when evil thoughts come into your mind, immediately turn your attention to God's promises and His love for you. The Bible says, "I have hidden your word in my heart that I might not sin against you" (Psalm 119:11).