Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does substr not return undef at the end of a string?

Tags:

substr

perl

I'm not sure whether this is defined behaviour or not. I have the following code:

use strict;
use warnings;
use Data::Dumper;

my $string = 'aaaaaa0aaaa';
my $char = substr($string, length($string), 1);
my $char2 = substr($string, length($string)+1, 1);

print Dumper($char);
print Dumper($char2);

Besides getting one warning about substr() past the end of a string, I'm confused about the output:

$VAR1 = '';
$VAR1 = undef;

Perldoc says about substr:

substr EXPR,OFFSET,LENGTH

If OFFSET and LENGTH specify a substring that is partly outside the string, only the part within the string is returned. If the substring is beyond either end of the string, substr() returns the undefined value and produces a warning.

Both length($string) and length($string) + 1 are beyond the (zero-indexed) end of the string, so I don't know why substr returns the empty string in one case and undef in the other. Does it have to do with the NULL character that C uses for string termination and that is somehow returned by substr in the first case, so that there is an "invisible" last character to this string that is not counted by length? Am I missing something obvious here?

like image 493
mpe Avatar asked Oct 26 '12 11:10

mpe


People also ask

What does Substr return?

The SUBSTR function returns a portion of string, beginning at a specified character position, and a specified number of characters long. SUBSTR calculates lengths using characters as defined by the input character set. To retrieve a portion of string based on bytes, use SUBSTRB.

How do you return part of a string?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

Is Substr deprecated in JavaScript?

substr(…) is not strictly deprecated (as in “removed from the Web standards”), it is considered a legacy function and should be avoided when possible. It is not part of the core JavaScript language and may be removed in the future. If at all possible, use the substring() method instead.

How do substring () and substr () differ in JavaScript?

The difference between substring() and substr()The two parameters of substr() are start and length , while for substring() , they are start and end . substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 .


1 Answers

There are a couple of issues here. Firstly you should consider the substr offset to indicate position between characters thus:

 S T R I N G
0 1 2 3 4 5 6

so you can see that offset 6 - the length of the string - is at the end of the string, not beyond it.

Secondly the length parameter of substr serves as an upper limit to the number of characters returned, not a requirement. That is what the documentation means by only the part within the string is returned.

Putting these together, a call like substr 'STRING', 6, 1 - asking for a maximum of one character at the end of the string - returns the empty string, while asking for anything beyond the end of the string (or before its start) gives undef.

like image 57
Borodin Avatar answered Sep 25 '22 03:09

Borodin