We humans have not been created infallible. We have some capabilities in us, in our fitra and that is to worship Allah however we are not infallible and are prone to making mistakes. There are only a few chosen people who are infallible however the rest of us have the possibility of making mistakes, wrongdoings and even committing a sin. Therefore if a person commits a sin, first she/he should not rebuke herself/himself but should be hopeful of Allah's mercy. When I say rebuke, I mean regretting the past and becoming hopeless about the future as well. However rebuking of the soul for committing a wrong and going against the wishes of Allah is good (without becoming hopeless and only to correct oneself).
A person should know that when they sin, shaytan comes to them and encourages one to become hopeless of Allah mercy. When one gets thoughts where he blames himself for a sin committed and becomes hopeless of Allah's mercy, these are satanic thoughts.
In cases of masturbation, many times, the thoughts a person gets, thoughts where a person blames themselves, these become the cause for a person being unable to leave the sin (of masturbation) and continuously gets caught in a circle of committing the same act.
Apart from this, a person should control their thoughts and should try to correct them. There are a few things that a person should observe through which the paths will open to gain the strength to let go of a particular act/sin:
-Diet – Pay attention to your diet. Try to eat at particular times the right meals where the food is not a lot and it should not be very strong. The food should be less and enough to eliminate one's hunger. The meals should be taken at specific times and outside of these times, extra food should not be consumed hence gaining control over one's food. Foods which have strong/heavy spices and a lot of meat, especially red meat increase the problem hence consume foods which have less meat and should not have strong spices. You can take a meal plan from a dietician.
– A person should take part in specific activities such as professional sports such as running, swimming. The sport should be one where a person would get very tired
– A person should also take part in professional artistic activities. Any activity that you like should be done at a specialised and professional level.
In situations such as yours, professional sports and arts play a very important role.
– Fasting. Try to fast every Monday and Thursday. Fasting is very effective in controlling and subsiding the attraction to masturbation and the thrill after.
– One should write past memories in exact detail on paper and then tear it up and throw it away. Do that again for the second time. Write in detail and tear up the paper and throw it away every time you get thoughts of the past. Do this as many times as needed till the thoughts of the past do not disturb you anymore. After this, anything that comes up that bring those thoughts to you, try to repeat this sentence 'Whatever has happened however from now on, Allah can help me. Whatever happened was not in my hand and whatever problem I have got but now Allah can help me and from now on my life is in my hands.' Also recite verses of the Qur'an to yourself and repeat those sentences and try to know/understand that whatever is not in one's hand, God does not punish therefore from now on try to correct your actions and work on the paths to letting go of this particular act/sin of masturbation.
– Early marriage. It is better for those who masturbate to get married early. Reduce a bit expectations one has of marriage and get married quickly and there is absolutely no need to inform one's partner of the past.
– Select friends who will help you go towards Allah and cause one to not go towards sin as it has been mentioned (in the narrations) to sit with those who remind you of Allah. Hence friends should be religious people and those who are not religious should be left.
– Another thing that can help a person to move away from masturbation is medicines. A psychiatrist who you can refer to will be able to suggest the correct medication to help with controlling the urge to masturbate.
– Just making a promise to Allah to not to commit the sin again is not very helpful since ever everytime a person returns back to the act, she/he will rebuke themselves. The above tips mentioned will be helpful but don't make a promise with Allah. Instead keep a penalty for yourself such as one of monetary value. So for example you keep a penalty for yourself that if you do this act again, you will give a specific amount in sadaqa. Or the penalty can be something that is hard for you to do or something that you don't enjoy doing. Therefore instead of making a promise with Allah, make a promise to yourself that if you do this act again, you will penalise yourself with a specific penalty. This will be more helpful. Insha'Allah Allah will help you in letting go of this act.