Definitely sorry to hear that your ex won’t leave you alone and is sending late night text messages to you that, more than anything, probably reinforce that breaking up with him was such a good idea. Ah, relationships, they’re never easy! In the old days you would have been basically out of luck with unwanted text messages because while the phone company could begrudgingly set up a phone number block, that wouldn’t have helped with SMS texting anyway.
The good news is that Apple has incorporated a number blocking technology into the latest version of its iOS system for the Apple iPhone line and if your phone’s up-to-date then you’ll have this capability too. This also blocks phone calls and FaceTime chats too, so it’s rather a one-size-fits-all solution from Apple.
To block a phone number from sms text message texting you, start in Messages where you can see the message:

I’m going to demonstrate by blocking text messages from Yelp. You can see one on the second line, from +1 (763) 762-9357. Of course, Yelp only sends these messages when you explicitly request them, but it’s a demo, right? Let’s roll with it…
A tap and the text messages and dialog are shown. This is all standard iOS stuff at this point:

Here’s where it gets interesting! Instead of just grumbling and swearing at your phone 🙂 tap on the “i” in a circle on the top right of your Apple iPhone 7 screen. It brings up this view:

You can tap on “Do Not Disturb” but it’s a bit confusing: Apple says it’s just for temporarily blocking a line, but typically Do Not Disturb is a darn useful iPhone feature best used late at nigh [here’s a tutorial on how to work with that mode: Enable late night sleep quiet mode, apple iphone]. Confusing.
That’s not what we want anyway so tap on the “>” adjacent to the phone icon on the top right.
Now you’ll be at the Contacts entry and it should look like this:

See the very last option? “Block this Caller“. That’s what you want! In fact, even if you already have an address book / contact entry for your ex-boyfriend (or anyone else you want to block) you’ll find this option is available, so it’s pretty easy to block him whether you’ve purged him from your iPhone world or not!
Finally, tap on it and you get a bit more info:

Perfect. Oh, and if you’re worried, Apple assures us that “the contact won’t get a notification that the call or message was blocked”.
Good luck!