function s = extractBefore(str, pos)
% EXTRACTBEFORE Extract substring before specified position
%    S = EXTRACTBEFORE(STR, PAT) returns a substring of STR that starts
%    with the first character of STR and ends with the last character
%    before the first occurrence of PAT.
%
%    S = EXTRACTBEFORE(STR, POS) returns a substring of STR that begins
%    with the first character of STR and ends with the last character
%    before the numeric position specified by POS. POS must be an integer
%    between 1 and the number of characters in STR.
%
%    NOTE: STR must be text, which means it can be a string array, a character 
%    vector, or a cell array of character vectors. PAT must be text or a 
%    pattern array. The output S has the same data type as STR.
%
%    Examples:
%        STR = "The quick brown fox";
%        PAT = ' brown';
%        extractBefore(STR,PAT)
%
%        % returns "The quick"
%
%        STR = "The answer is 42!";
%        PAT = digitsPattern;
%        extractBefore(STR,PAT)
%
%        % returns "The answer is "
%
%        STR = 'peppers and onions';
%        POS = 8;
%        extractBefore(STR,POS)
%
%        % returns 'peppers'
%
%    See also EXTRACTAFTER, EXTRACTBETWEEN, PATTERN, DIGITSPATTERN

%   Copyright 2016-2020 The MathWorks, Inc.

    narginchk(2, 2);
    if ~isTextStrict(str)
        firstInput = getString(message('MATLAB:string:FirstInput'));
        error(message('MATLAB:string:MustBeCharCellArrayOrString', firstInput));
    end

    try
        s = string(str);
        s = s.extractBefore(pos);
        s = convertStringToOriginalTextType(s, str);
        
    catch E
        throw(E);
    end
end
