MatrixBerryCore
splitpart.m
Go to the documentation of this file.
1 function resStr=splitpart(inpStr,delimStr,fieldNum)
2 import mxberry.core.throwerror;
3 %
4 if ~(ischar(fieldNum)||isnumeric(fieldNum))
5  throwerror(':wrongInput',...
6  ['fieldNum parameter is expected to be either string or a ',...
7  'value of a numeric type']);
8 end
9 
10 strSplitList=strsplit(inpStr,delimStr);
11 nFound=length(strSplitList);
12 if nFound==0
13  throwerror('wrongInput',...
14  ['Opps, we shouldn''t be here - the number of ',...
15  'splitted strings is zero']);
16 end
17 %
18 if ischar(fieldNum)
19  switch lower(fieldNum)
20  case 'first'
21  fieldNum=1;
22  case 'last'
23  fieldNum=nFound;
24  end
25 end
26 %
27 if nFound<fieldNum
28  throwerror('wrongInput',...
29  ['a number of splitted strings (%d) is too small for a ',...
30  'specified position (%d)'],...
31  nFound,fieldNum);
32 end
33 %
34 resStr=strSplitList{fieldNum};
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function splitpart(in inpStr, in delimStr, in fieldNum)
SPLITPART splits string on delimiter and return the given field (counting from one) ...