Let's say I have two strings:
How do I match the "Test" at the end of string and only get the second as a result and not the first string. I am using include?
but it will match all occurrences and not just the ones where the substring occurs at the end of string.
You can do this very simply using end_with?
, e.g.
"Test something Test".end_with? 'Test'
Or, you can use a regex that matches the end of the string:
/Test$/ === "Test something Test"
"This-Test has a ".end_with?("Test") # => false
"This has a-Test".end_with?("Test") # => true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With