File: strjoin.m

package info (click to toggle)
octave-strings 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 452 kB
  • ctags: 29
  • sloc: makefile: 79; cpp: 67; sh: 25
file content (58 lines) | stat: -rw-r--r-- 2,065 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@uta.edu>
## 
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; If not, see <http://www.gnu.org/licenses/>.
##

## -*- texinfo -*-
## @deftypefn {Function File} @var{rval}= {} strjoin(@var{prefixstr},@var{stringcell})
## @deftypefnx {Function File} @var{rval}= {} strjoin(@var{prefixstr},@var{varargs})
## joins the strings in @var{stringcell} with the @var{prefixstr} like the list-join
## function in Python; the second version allows usage with variable number of arguments.
## Note that, if using cell-array as a second argument, only 2 arguments are accepted.
## Also note that, both the arguments are strings or containers of strings (cells).
##
## @example
## @group
##           strjoin(' loves-> ','marie','amy','beth') 
##           ##returns 'marie loves-> amy loves-> beth'
##
##           strjoin('*',@{'Octave','Scilab','Lush','Yorick'@})
##           ##returns 'Octave*Scilab*Lush*Yorick'
## @end group
## @end example
## @end deftypefn
## @seealso {strcmp}
##

function rval=strjoin(spacer,varargin)
  if (nargin < 2) || (nargin > 2  && iscell(varargin{1}) )
    print_usage();
  end

  if iscell(varargin{1})
     varargin=varargin{1};
  end

  rval="";
  L=length(varargin);
  for idx=1:(L-1)
    rval=strcat(rval,sprintf('%s%s',varargin{idx},spacer));
  end
  rval=strcat(rval,varargin{L});
  return;
end
%!
%!assert(strjoin("-","hello"),"hello")
%!assert(strjoin('*',{'Octave','Scilab','Lush','Yorick'}),'Octave*Scilab*Lush*Yorick')
%!