Home » Php » What is the Ruby equivalent of the PHP substr()?

What is the Ruby equivalent of the PHP substr()?

Posted by: admin July 12, 2020 Leave a comment

Questions:

What is the Ruby equivalent of the PHP statement

<?php echo substr("abcdefghijklm", 0, 5); ?>

How to&Answers:

Use the bracket notation, with the same two arguments as for substr.

substring = bigstring[0,5]

I used to recommend http://railsforphp.com/substr as a great way to find Ruby/Rails equivalents for common PHP functions, but apparently it’s no longer online. That’s unfortunate :/

Answer:

Use byteslice method

"abcdefghijklm".byteslice(0, 5) => "abcde"

https://ruby-doc.org/core-2.2.0/String.html#method-i-byteslice