Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

string

php

ruby

What is the Ruby equivalent of the PHP statement

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

like image 623
Mithun Sreedharan Avatar asked Nov 23 '10 04:11

Mithun Sreedharan


People also ask

What is substring in Ruby?

the Substring Method in Ruby A substring is a range or specific array of characters taken from an existing string.

What is Substr in PHP?

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.

Where is substring in Ruby?

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 .

How do I use contains in Ruby?

Syntax: str. include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false.


1 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 :/

like image 104
Matchu Avatar answered Oct 20 '22 00:10

Matchu