How do you stop sexual thoughts or impulses from arising?
Through analysis and study of what sexuality actually is. This includes completely devaluing the object of attraction (i.e. the person, thoughts, body parts, etc) to zero. You can do this by thinking about all the ways that the pleasure derived from sexuality is merely illusory, not real, and only temporary. When you are indulging in some form of sexuality, you tend to forget how filthy the human body actually is. For instance, you forget that every pore and opening in our body releases waste, which looks and smells awful. If faeces, sweat, and other discharge smell so bad, imagine how it would be inside the body. In addition, if there is real pleasure and happiness in physical contact and touch, then there should be pleasure even when your skin has an open wound or rash, but that is not the case. Furthermore, any form of dependency is the cause of suffering in this world, so how can dependency on someone else be the reason for happiness?
Analyze and know the real nature of sexual attraction
Here are some other ways in which you can analyze sexual attraction and see it as it is, in order to break the belief that there is happiness in sexuality:
Man has the wrong belief that pleasure exists in sex. Now, if you experience greater happiness and pleasure in something other than sex, then you would not seek pleasure in sex. There is no happiness in sex but there is no other option for the humans in worldly interactions. Otherwise, who would knowingly open the lid of a gutter? If happiness were there in sexuality, the emperor kings would not have departed for the search of true happiness in spite of having so many queens!
It is due to our wrong belief. It is only our belief. There is nothing except for our belief. Destroy that belief and there would not be anything.
So one must think about how filthy and dangerous sexual involvement is in order to stop sex and sexual attraction.