What ways are there to prevent oneself from going into sexual immorality? I have masturbated before and afterwards I repented. I realized that it's very wrong to do so. So now I want to avoid it, but like you said before that it can become a hard habit to break. Thanks to the Lord I have kept myself from doing to a while, but there have been times when I had that "want". Can you please tell me what I can do?
The Scriptures do not directly address the topic of masturbation, but the answer to an earlier question "Is masturbation unacceptable?" covers the available passages. While people debate whether masturbation is sinful, it is clear that stimulating yourself while thinking about committing acts of fornication or adultery would be wrong (Matthew 5:28). However, a more important factor in your case is that you consider it sinful. "Do you have faith? Have it to yourself before God. Happy is he who does not condemn himself in what he approves. But he who doubts is condemned if he eats, because he does not eat from faith; for whatever is not from faith is sin" (Romans 14:22-23). It is wrong for a Christian to do things he is not confident is acceptable to God.
Another person asked a related question to yours: "I'm addicted to pornography and masturbation: how do I stop?" Please take a moment to read that answer as it covers many side issues to your own question.
However, to directly address what you asked, the answer is both simple and complex: "Now concerning the things of which you wrote to me: It is good for a man not to touch a woman. Nevertheless, because of sexual immorality, let each man have his own wife, and let each woman have her own husband. Let the husband render to his wife the affection due her, and likewise also the wife to her husband. The wife does not have authority over her own body, but the husband does. And likewise the husband does not have authority over his own body, but the wife does. Do not deprive one another except with consent for a time, that you may give yourselves to fasting and prayer; and come together again so that Satan does not tempt you because of your lack of self-control. But I say this as a concession, not as a commandment. For I wish that all men were even as I myself. But each one has his own gift from God, one in this manner and another in that. But I say to the unmarried and to the widows: It is good for them if they remain even as I am; but if they cannot exercise self-control, let them marry. For it is better to marry than to burn with passion" (1 Corinthians 7:1-9). If you have a strong sex drive, then find the proper outlet for it by getting married.
What do you do in the meantime? Too many Christians approach the problem of sin by only removing the sinful action. That is only half the problem. To only cease sinning leaves a void in our lives that wants to be filled (Matthew 12:43-45). Such a void will be filled and too often it does get filled by sin when we tire out from trying to resist temptation. The solution is to actively fill you life with righteous things so that there is no room left for sin. You find this all through the New Testament. Christians are told to remove sinful actions and replace them with righteous ones (Ephesians 4:20-32; Colossians 3:5-17). "And do this, knowing the time, that now it is high time to awake out of sleep; for now our salvation is nearer than when we first believed. The night is far spent, the day is at hand. Therefore let us cast off the works of darkness, and let us put on the armor of light. Let us walk properly, as in the day, not in revelry and drunkenness, not in lewdness and lust, not in strife and envy. But put on the Lord Jesus Christ, and make no provision for the flesh, to fulfill its lusts" (Romans 13:11-14). So when the desire comes to do something you believe is wrong, actively search out something righteous that you can do instead.