北海道苫小牧市出身の初老PGが書くブログ

永遠のプログラマを夢見る、苫小牧市出身のおじさんのちらしの裏

いろんなsubstring

substring関数ってなんでこんなに言語によって違うんでしょう?

[SQL(Oracle)]
SQL> select substr('abcdefghijklmn', 5, 2) from dual;
↓
SU
--
ef

SQL> select substr('abcdefghijklmn', 2, 5) from dual;
↓
SUBST
-----
bcdef




[JavaScript]
document.write("abcdefghijklmn".substring(5, 2) + "<BR />");
document.write("abcdefghijklmn".substring(2, 5) + "<BR />");
↓
cde
cde




[Perl]
C:\Documents and Settings\10248351.FBD010200097085>perl
print substr("abcdefghijklmn", 5, 2), "\n";
print substr("abcdefghijklmn", 2, 5), "\n";
↓
fg
cdefg




[Java]
        System.out.println("abcdefghijklmn".substring(2, 5));
        System.out.println("abcdefghijklmn".substring(5, 2));
↓
cde
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -3
	at java.lang.String.substring(Unknown Source)
	at SubString.main(SubString.java:16)

覚えきれぬ。