What is the Ruby equivalent of the PHP statement
<?php echo substr("abcdefghijklm", 0, 5); ?>
the Substring Method in Ruby A substring is a range or specific array of characters taken from an existing string.
substr in PHP is a built-in function used to extract a part of the given string. The function returns the substring specified by the start and length parameter. It is supported by PHP 4 and above. Let us see how we can use substr() to cut a portion of the string.
Use the [] Syntax to Check Whether a String Contains a Substring in Ruby. For accessing a character in a string at a specific index, we could use the [] syntax. The interesting thing about this syntax is that if we pass in a string, it will return a new string that is matched; otherwise, it will return nil .
Syntax: str. include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false.
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 :/
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