There's no way to do it other than just doing it, I guess. I was on probation for a felony charge (controlled substance related) and I quit all substances for a year (other than cigarettes...I actually reached the height of my nicotine addiction while on probation, ironically probably doing more damage to my health than had I not been on probation. Luckily I quit smoking about 4 or 5 years ago). At that time I kept off drugs with the thought of "I get random drug screenings 6 days out of the week and I don't want to go to prison for a dirty UA"...
I was miserable the whole time, though, and immediately started doing drugs again once my probation ended.
If you want to make a long-term goal out of quitting drugs, and not just a temporary, I-don't-wanna-get-incarcerated hiatus like I took, I'd suggest that you spend some time thinking hard about what you're passionate about. Speaking personally, besides drugs, there are really only a couple topics that I'm genuinely passionate about. One is my academic field (History...I love reading about and studying the past) and the other is my career (commercial fishing). Whenever someone asks what I do in life, I reply "I'm a fisherman" (I certainly wouldn't reply, "I'm a drug user!" LOL) That's what I love to do...that's what gives meaning and purpose to my life, being on the ocean and catching fish. If I didn't have my career I'd probably go completely out-of-my-mind apeshit crazy and end up on the evening news
As I've said in another thread, that's really one of the crucial things people need to have, is some meaning they've constructed for themselves in what is probably a meaningless existence (LIFE). Whether it's participation in a support group of other substance (ab)users, or volunteering your time somewhere, or a job you're passionate about, or a religion / political movement you're affiliated with, or simply a hobby / pastime that you really enjoy & which you find healthy / good for your own state of mind...gotta have something like that in your life. Because, just speaking IMO/IME, drugs do not add meaning to one's life (although admittedly I love drugs, both as an intellectual topic to study & "practically", as things to use and get high on).