Logo

Programming-Idioms

History of Idiom 38 > diff from v23 to v24

Edit summary for version 24 by :

Version 23

2015-09-28, 20:52:35

Version 24

2015-10-29, 14:05:13

Idiom #38 Extract a substring

Find substring t consisting in characters i (included) to j (excluded) of string s.
(character indexes start at 0 unless specified otherwise)

Idiom #38 Extract a substring

Find substring t consisting in characters i (included) to j (excluded) of string s.
(character indexes start at 0 unless specified otherwise)

Code
String.slice(string, i..j-1)
Code
String.slice(string, i..j-1)
Code
$t = mb_substr($s, $i, $j-$i, 'UTF-8');
Code
$t = mb_substr($s, $i, $j-$i, 'UTF-8');
Comments bubble
Requires multibyte (mb) extension for unicode support.
Comments bubble
Requires multibyte (mb) extension for unicode support.
Code
my $chunk = substr("now is the time", i, j);
Code
my $chunk = substr("now is the time", i, j);
Comments bubble
Perl is 0-based: i=3 would start at the 4th character.
Comments bubble
Perl is 0-based: i=3 would start at the 4th character.
Code
var t = s.substring(i, j);
Code
var t = s.substring(i, j);
Doc URL
https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-core.String#id_substring
Doc URL
https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-core.String#id_substring
Code
t = s[i:j]
Code
t = s[i:j]
Comments bubble
Slicing works on strings.
Comments bubble
Slicing works on strings.
Origin
http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python/663175#663175
Origin
http://stackoverflow.com/questions/663171/is-there-a-way-to-substring-a-string-in-python/663175#663175
Code
t = s[i..j-1] 
Code
t = s[i..j-1] 
Comments bubble
In ruby j is included by default
s = "test"
t = "s[0..1] => "te"
Comments bubble
In ruby j is included by default
s = "test"
t = "s[0..1] => "te"
Doc URL
http://www.ruby-doc.org/core-2.1.1/String.html#method-i-slice
Doc URL
http://www.ruby-doc.org/core-2.1.1/String.html#method-i-slice
Code
String t = s.substring(i,j);
Code
String t = s.substring(i,j);
Comments bubble
Throws IndexOutOfBoundsException if i is negative, or j is larger than the length s, or i is larger than j.
Comments bubble
Throws IndexOutOfBoundsException if i is negative, or j is larger than the length s, or i is larger than j.
Demo URL
http://ideone.com/R8U6Pv
Demo URL
http://ideone.com/R8U6Pv