]> Creatis software - CreaPhase.git/blob - octave_packages/strings-1.1.0/strjoin.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / strings-1.1.0 / strjoin.m
1 ## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@uta.edu>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{rval} =} strjoin (@var{prefixstr}, @var{stringcell})
18 ## @deftypefnx {Function File} {@var{rval} =} strjoin (@var{prefixstr}, @var{varargs})
19 ## Joins the strings in @var{stringcell} with the @var{prefixstr} like the list-join
20 ## function in Python; the second version allows usage with variable number of arguments.
21 ## Note that, if using cell-array as a second argument, only 2 arguments are accepted.
22 ## Also note that, both the arguments are strings or containers of strings (cells).
23 ##
24 ## @example
25 ## @group
26 ##           strjoin(' loves-> ','marie','amy','beth') 
27 ##           ##returns 'marie loves-> amy loves-> beth'
28 ##
29 ##           strjoin('*',@{'Octave','Scilab','Lush','Yorick'@})
30 ##           ##returns 'Octave*Scilab*Lush*Yorick'
31 ## @end group
32 ## @end example
33 ## @seealso {strcmp}
34 ## @end deftypefn
35
36 function rval = strjoin (spacer, varargin)
37   if (nargin < 2) || (nargin > 2  && iscell(varargin{1}) )
38     print_usage();
39   end
40
41   if iscell(varargin{1})
42     varargin=varargin{1};
43   end
44
45   rval="";
46   L=length(varargin);
47   for idx=1:(L-1)
48     rval=strcat(rval,sprintf('%s%s',varargin{idx},spacer));
49   end
50   rval=strcat(rval,varargin{L});
51 endfunction
52
53 %!assert(strjoin("-","hello"),"hello")
54 %!assert(strjoin('*',{'Octave','Scilab','Lush','Yorick'}),'Octave*Scilab*Lush*Yorick')