I was once in a rehab where they taught us how to deal with craving for alcohol. The 5 points that they told us about are:
Drink – water or juice. It acts as a substitute for alcohol and helps decrease the craving. I am not sure this works very well for drugs but it does work to some extent for alcoholism.
Distract – look for distractions to take your mind off alcohol and drugs. Watching tv or surfing on the internet works well. I have found that writing a blog is also a good distraction.
Discuss – discuss about your craving with someone you trust. If you are lucky enough to have someone you love and trust, discuss about what you are going through. This helps a lot.
I think it helps to know that you are craving and to admit this to yourself. You need to tell yourself that craving is just a natural phenomenon, like the symptoms of a disease. It is difficult to ignore craving but once you have identified it for what it is, you are better equiped to deal with it.