part
Extraction of characters from strings
Syntax
strings_out = part(strings_in, v)
Arguments
- strings_in
a character string or a matrix of character string.
- v
a vector of integer values containing the indices of characters to be extracted.
$
is accepted and means length(strings_in).- strings_out
a character string or matrix of character string.
Description
This function extracts characters from strings. The characters to be
extracted are referred to by their indices contained in v
.
strings_out
is filled with whitespace characters when indices
are beyond the input string's length.
v
may contain $
symbol which stands for the
length of string_in
.
Examples
// Returns characters position 8 to 11 part("How to use ""part"" ?", 8:11) // Returns characters position 2 to 4 for each element // No characters replaced by '' c = part(['a', 'abc', 'abcd'], 2:4) // Returns character position 1 for each element and add characters position // 4 to 7 of each element c = part(['abcdefg', 'hijklmn', 'opqrstu'], [1, 4:7]); // Returns character 4 for each element, add characters position 1 to 7 and // add character position 4 for each element c = part(['abcdefg', 'hijklmn', 'opqrstu'], [4, 1:7, 4]); // Returns character position 1, add again character position 1 and // character position 2 c=part(['a', 'abc', 'abcd'], [1, 1, 2]) part(['a', 'abc', 'abcd'], [1]) // => ['a' 'a' 'a'] part(['a', 'abc', 'abcd'], [1 1]) // => ['aa' 'aa' 'aa'] part(['a', 'abc', 'abcd'], [1 1 2]) // => ['aa ' 'aab' 'aab'] // Repeating a character N times: N = 10; part('-', ones(1:N)) // => '----------' // Repeating a pattern N times: N = 6; pat = '- '; part(pat, ones(1:N).*.(1:length(pat))) // => '- - - - - - ' // Using $ = implicit length of strings: // 1) part(['a string' 'another longer one'], $-2:$ ) // => [ 'ing' 'one'] // 2) Another implementation for strrev(): part('Hello world', $:-1:1) // => 'dlrow olleH' // 3) With unranging $: part('Hello world', [ $ 4:5 ]) // => 'dlo' // 4) Mixing scalar or unranging $ with ranging ones is not possible: part("Hello", [ 1 $-1:$ $ ]) // => error
See also
History
Version | Description |
5.5.0 | $ standing for length(input_strings) is now accepted in indices of selected characters |
Report an issue | ||
<< length | Strings | prettyprint >> |