Logo

Programming-Idioms

History of Idiom 46 > diff from v19 to v20

Edit summary for version 20 by :

Version 19

2015-10-08, 12:53:53

Version 20

2015-10-29, 14:05:14

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Idiom #46 Extract beginning of string (prefix)

Create string t consisting of the 5 first characters of string s.

Imports
import qualified Data.Text as T
Imports
import qualified Data.Text as T
Code
t :: T.Text
t = T.take 5 s
Code
t :: T.Text
t = T.take 5 s
Comments bubble
This implementation uses the Text type, imported as suggested in the Text library documentation.
Comments bubble
This implementation uses the Text type, imported as suggested in the Text library documentation.
Doc URL
https://hackage.haskell.org/package/text
Doc URL
https://hackage.haskell.org/package/text
Code
t :: String
t = take 5 s
Code
t :: String
t = take 5 s
Comments bubble
The function take has two arguments: the number of characters to take as a prefix, and the string to take the prefix from. The function take is in the Haskell prelude and doesn't need to be imported.

The type signature is optional, and could be omitted leaving the second line.
Comments bubble
The function take has two arguments: the number of characters to take as a prefix, and the string to take the prefix from. The function take is in the Haskell prelude and doesn't need to be imported.

The type signature is optional, and could be omitted leaving the second line.
Code
var t = s.substring(0, 5);
Code
var t = s.substring(0, 5);
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
var t = s.substring(0,5);
Code
var t = s.substring(0,5);
Comments bubble
s.substr(0,5) would also work in that specific case.
Comments bubble
s.substr(0,5) would also work in that specific case.
Doc URL
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring
Doc URL
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substring
Code
String t = s.substring(0,5);
Code
String t = s.substring(0,5);
Comments bubble
s must not be null.
Comments bubble
s must not be null.