Strings and substrings

Hello,

str = "hello world"
str[0] # -> 104

What does ‘str[0]’ do? Why does it print 104 instead of “h”?

I realize that these work:

str[0…0] # -> "h"
str[0,1] # -> “h”

Could someone explain to me what these do? And why I get:

str[0,0] # -> “”

Thanks a lot,
Daniel.

Hello,

str = “hello world”
str[0] # → 104

it’s the ASCII code of the caractere

What does ‘str[0]’ do? Why does it print 104 instead of “h”?

I realize that these work:

str[0…0] # → “h”
str[0,1] # → “h”

ok, it’s the substring

Could someone explain to me what these do? And why I get:

str[0,0] # → “”

and str[0,1]

···

On Wed, 2002-11-27 at 17:17, Daniel Carrera wrote:

Thanks a lot,
Daniel.


Ludo coquelle@enib.fr
ENIB/LI2

Hello,

str = “hello world”
str[0] # → 104

What does ‘str[0]’ do? Why does it print 104 instead of “h”?

I realize that these work:

str[0…0] # → “h”
str[0,1] # → “h”

Could someone explain to me what these do? And why I get:

http://www.glue.umd.edu/~billtj/ruby.html#characters

str[0,0] # → “”

substring starting at index 0, length 0 => empty string

···

On Thu, Nov 28, 2002 at 01:17:15AM +0900, Daniel Carrera wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

C is quirky, flawed, and an enormous success
– Dennis M. Ritchie

str = “hello world”
str[0] # → 104

What does ‘str[0]’ do? Why does it print 104 instead of “h”?

104 is the ASCII code of ‘h’.

str[0…0] # → “h”

The range provides start and ending positions. The substring between these
positions (inclusive) is returned. In this case: from position 0 to
position 0.

str[0,1] # → “h”

Here, the first parameter is the starting position. The second the length
of the desired substring.

Could someone explain to me what these do? And why I get:

str[0,0] # → “”

You asked for a substring starting at position 0 of length 0.