Hey Tnereffid. I know someone who is going through exactly the same struggle. They are a really strong faith and they love The Lord, but they can't seem to break the habit. They would come and tell me how guilty they felt about it afterwards and how trapped they felt. Though what I tell you might not give you instant results (my friend is still overcoming it), it might give you encouragement and help you with the long term result.
Now the first thing that I will address is the shame and guilt that often follows... I know that it feels horrible knowing that you've messed up again and that no matter how many times you promise to yourself and God that you'll do better next time, that you seem to just mess up all over again (it's important to remember that all Christians experience times where they've messed up: even the great apostle Paul wrote "I have the desire to do what is good, but I cannot carry it out. For I do not do the good I want to do, but the evil I do not want to do - this I keep on doing. Romans 7:19). Also, we need to remember just how loving and forgiving God is... We know that sin is wrong and that we must try to live upright lives, but we wouldn't expect God to stop loving a Christian because they told a lie, and in gods eyes, almost all sin is the same... Even after king David committed adultery, God called him a man after God's own heart.
Now whether masturbating is actually wrong, is a question that is being debated quite a bit, with opinions being held at both ends. I don't think the actual act of doing it is wrong as it is literally nothing said about it or against it (it's very easy for us to start coming up with new rules and so on... much like the Pharisees did). However, though it in itself might not be wrong, it could lead to sin that is very much spoken of in the bible (such as lust and impure thoughts). An example of such is with alcohol... It never says in the bible that you can't drink it, however it does talk about how we should try and remove things from our lives that are a temptation, and for many it is very difficult not to go out and get drunk, which is spoken against in the bible. So it depends on the motive behind the act and whether the act does lead or stem from sin.
Now if what you're doing is a result of sin, it does need to be stopped... and this can be a long process as can be highly addictive. It's important to realise however that it's not so much the sin itself that is so bad, but the sin in our hearts an minds... Doing sinful things is merely the symptoms of that underlying problem. So the thing that really needs to take to take precedents isn't so much the need to stop doing it, as it is the need to fix the issue at heart. When we have our mind, soul and spirit straightened out, following The Lord will happen naturally.
So the first thing that I'd recommend is talking to God about it, and asking him to really reveal what the problem is (maybe you're doing it to relieve stress or anxiety which could possibly point to an underlying lack in trustingGod to look out for you? Maybe something else). And to then ask him to deal with that underlying issue... If you ask God earnestly and seek him to help you, he will... But sometimes it does take time.
If you're doing it as a result of impure thoughts or seeing things such as pornography, then I recommend you also pray about God helping to purify your thoughts. I personally went through a stage where I really struggled with impure thoughts, and what I find helps is to read and memorising it. Also, I know that this might sound really strange, but I found that whenever my mind would stray, I would picture ripping or stabbing that thought in my mind like a paper... and that really helped. Also, try and remove yourself from any pornography. I know it's all over the place, but take measures: unfollow pages that sometimes have it, put the add block on to prevent pop-ups, etc. in this you will be transformed by the renewing of your mind (Romans 12:2).
Now I know that what I say next you probably won't like, but sometimes I with habits like these the journey to breaking them isn't fun. A while ago I read the book Freedom from Addiction, and it describes how a man who was a serious alcoholic had to undergo quite a bit of hardship to finally break free. In the end he said that it took him till he finally realised that only God's power could free him, and that his strength alone couldn't do it. That's why it is so difficult for people to break addictions: they think that they can break out of it themselves, not realising that sin is stronger than us and that only through God's strength can they overcome it (that's why I recommended you to pray about it).
The reason why I said before that it could take a while is because though we might know in our minds that we need to rely on God and not ourselves, we don't know that in our hearts yet (which is actually what our will follows after). And sometimes it takes a lot for our hearts to learn that...for the man in the book, it took him till he was rock bottom to work out that he couldn't do it for himself. However, once he did find that out through experience, he could safely say that he was a much stronger Christian at the end because he had to learn how to rely on God so much; for people without obvious addictions, they could miss that level of dependence on God because they don't have reason to.
My last bit of advice is to find people you trust and tell them about your struggle (though I understand if you can't), and to ask them to pray for you regularly (therefore confess your sins to each other so that you may be healed. The prayer of a righteous person is powerful and effective. James 5:19).
anyways, this looks to be about half a novel long, so I'll leave it there. I really hope this helps and gives you encouragement! you'll be in my prayers.