I used to count till 10 repeatedly in my head while visualizing the numbers. At the beginning I had to do it a lot, but the thought started going out gradually and I got better at getting rid of them without the methods.
I also had the same issue with fantasizing. I usually fantasizing on bed when I am about to sleep. Now I think this is rather risky thoughts. I admit it, My weakness lies on bed (no pun intended).