String startsWith function in Octave
This function is the equivalent of Python’s startsWith function and works for strings:
startsWith.m
:
% Check if a string starts with a given prefix
% Returns 1 if s starts with prefix, 0 else
function retval = startsWith(s, prefix)
n = length(prefix);
if n == 0 # Empty prefix
retval = 1; # Every string starts with empty prefix
return
endif
retval = strncmp(s, prefix, n);
endfunction
Examples:
>> startsWith("myString", "my")
ans = 1
>> startsWith("myString", "abc123")
ans = 0
>> startsWith("myString", "My")
ans = 0
>> startsWith("myString", "")
ans = 0