Scilab Home Page | Wiki | Bug Tracker | Forge | Mailing List Archives | Scilab Online Help | ATOMS
File : Details
Login with GitLab

strrepeat()

Concatenates a string with itself N times or up to N characters
(1497 downloads for this version - 1497 downloads for all versions)
Details
Version
1.0
Author
Samuel Gougeon
Maintainer
Samuel Gougeon
Category
License
Supported Scilab Version
5.5
Creation Date
July 26, 2015
Description
            You like blanks() but life isn't made only of blanks. You will love strrepeat()

strrepeat() concatenates a character or a string or a set of strings or by
default a blank, respectively with themselves, a given number of times, or up to
a given number of characters or up to the length of a given text template. 

See below for a full help, description, and examples.

RESULTS OF EXAMPLES
-------------------
-->// Blanks:
--> '|' + strrepeat(9) + '|'
 ans  =
 |         |   
 
--> '|' + strrepeat([ 2 5 ; 4 8]) + '|'
 ans  =
!|  |    |     |     !
!|    |  |        |  !
 
--> '|' + strrepeat(['abc' ; '1234567']) + '|'
 ans  =
!|   |      !
!|       |  !


-->// Text with UTF-8 characters:
-->strrepeat('éüçñ実またちこんфий', 2)
 ans  = 
 éüçñ実またちこんфийéüçñ実またちこんфий   


-->// One text, various repeating rates:
--> strrepeat('.', [2 5 ; 0 8])
 ans  =
!..  .....     !
!    ........  !
 
--> strrepeat('-.', [3 -3 ; 5 -5])
 ans  =
!-.-.-.      -.-    !
!-.-.-.-.-.  -.-.-  !
 
--> strrepeat('.', ['abc' '1234567'])
 ans  =
!...  .......  !
 
--> strrepeat(['A' ''], -7)  // => error
 !--error 10000 
strrepeat: repeating the empty "" can't reach 7 characters!
 

-->// One repeating rate, various texts:
--> strrepeat(['-' '~' ; '-.' '~.'], 5)
 ans  =
!-----       ~~~~~       !
!-.-.-.-.-.  ~.~.~.~.~.  !
 
--> strrepeat(['-' '~' ; '-.' '~.'], -9)
 ans  =
!---------  ~~~~~~~~~  !
!-.-.-.-.-  ~.~.~.~.~  !
 
--> strrepeat(['-' '~' ; '-.' 'ab'], 'abcdefg')
 ans  =
!-------  ~~~~~~~  !
!-.-.-.-  abababa  !
 

-->// Element-wise repeating rates:
--> '|' + strrepeat(['' '-.' '/\' '0-' 'cB'], [8 5 0 4 -11]) + '|'
 ans  =
!||  |-.-.-.-.-.|  ||  |0-0-0-0-|  |cBcBcBcBcBc|  !

------------------------------------------------------------------

-->strrepeat

 Repeats a text pattern n times or up to a n-characters length

SYNTAX
     strrepeat         //  Displays this help
 R = strrepeat(N)      //  R(I) = N(I) blanks
 R = strrepeat(ST)     //  R(I) = length(ST(I)) blanks
 R = strrepeat(T,  N)  //  R(I) = T(I) repeated N(I)>0 times
                       //  R(I) = T(I) repeated up to |N(I)<0| characters
 R = strrepeat(T, ST)  //  R(I) = T(I) repeated up to length(ST(I)) characters
                  // ... where I stands for i, or (i,j), or (i,j,k..)
PARAMETERS
 T : Scalar, vector, matrix or hypermatrix of text to be repeated.
     By default, T = " ". UTF-8 characters are accepted.
 N : Scalar, vector, matrix or hypermatrix of integers.
     If N is a scalar while T is not so, N is used for every T components.
     If T is a scalar while N is not so, T is repeated according to
      each N component.
     If N and T are not scalars, they must have the same sizes.
 ST: Scalar, vector, matrix or hypermatrix of texts used as templates,
      setting N = length(ST).
     If T and ST are not scalars, they must have the same sizes.
 R : Scalar, vector, matrix or hypermatrix of repeated text.
     R has the sizes of the largest input T, N, or ST.
     R = []  if T, N or ST is [].

DESCRIPTION
 From a character, a word or a text, or from each component of a
 matrix of chars and words and texts, strrepeat() concatenates it
 with itself a) either as many times n as specified, or b) as many
 times as to reach a given number n of characters. In the latter
 case, if n is not a multiple of the item's length, the result of
 the concatenation is trimmed on the right to n characters.
 When no item to repeat is specified, " " is considered. Hence,
  - strrepeat(n) is equivalent to blanks(n). However,
  - strrepeat() is vectorized, while blanks() is not.
  - strrepeat() is applicable to any character or string, whereas
    blanks() generates exclusively white strings.
  - strrepeat() can take strings as templates, while blanks() cannot.

 NOTE: UTF-8 characters are supported.
 WARNING: If a text is empty and must be repeated to reach a non
  null length, an error occurs.

SEE ALSO
 blanks  : http://help.scilab.org/docs/5.5.2/en_US/blanks.html
 strcat  : http://help.scilab.org/docs/5.5.2/en_US/strcat.html
 strsubst: http://fileexchange.scilab.org/toolboxes/294000

REFERENCE:
  Comments, scoring and bug reports are welcome on
  http://fileexchange.scilab.org/toolboxes/365000

EXAMPLES
// Blanks:
 '|' + strrepeat(9) + '|'
 '|' + strrepeat([ 2 5 ; 4 8]) + '|'
 '|' + strrepeat(['abc' ; '1234567']) + '|'

// Text with UTF-8 characters:
 strrepeat('éüçñ実またちこんфий', 2)

// One text, various repeating rates:
 strrepeat('.',  [2 5 ; 0 8])
 strrepeat('-.', [3 -3 ; 5 -5])
 strrepeat('.',  ['abc' '1234567'])
 strrepeat(['A' ''], -7)  // => error

// One repeating rate, various texts:
 strrepeat(['-' '~' ; '-.' '~.'], 5)
 strrepeat(['-' '~' ; '-.' '~.'], -9)
 strrepeat(['-' '~' ; '-.' 'ab'], 'abcdefg')

// Element-wise repeating rates:
 '|' + strrepeat(['' '-.' '/\' '0-' 'cB'], [8 5 0 4 -11]) + '|'
            
Files (1)
[11.33 kB]
Miscellaneous file
Macro defining the function strrepeat()

* To use it in your current session, enter:
  exec("[path_to_strrepeat]/strrepeat.sci", -1); // and that's it

  // where [path_to_strrepeat] is the absolute path of the directory where 
  // you put strrepeat.sci

* MAKE IT AVAILABLE in EVERY SESSION:

  1) Prepare a directory where you put all .sci files of personal functions 
     that you want to load in session at startup. We will call it 'MyBag'. 
     Let's call MyBag_dir the absolute path refering to this directory.
  2) Put the file strrepeat.sci in it.
  3) In the console, enter: genlib('mybag_lib', MyBag_dir, %t)
     (replace 'MyBag_dir' with the path)
  4) Edit your personal startup file. In the console, enter:
     edit SCIHOME/scilab.ini    // if your OS is MS Windows
     edit SCIHOME/.scilab       // otherwise (Linux, Mac OS, etc)
  5) AT THE BEGINNING of the startup file, insert theses 2 lines:
     mybag_lib = lib(MyBag_dir);  // replace MyBag_dir with the path
     // tmp = predef(); predef(tmp+1); clear tmp // comment due to bug 8634
  6) Save the startup file. 
  7) Quit and restart Scilab. 

    What we have done is building a library of all your favorite functions.
    This is what genlib() does. Then, in the startup file, the library is 
    loaded in session (by lib(..)), and protected against further 'clear'
    instructions (by predef(..)).

    In the new session, you may test that strrepeat() is available: 
    Enter: strrepeat  // The help page should be displayed in console.

News (0)
Comments (0)
Leave a comment
You must register and log in before leaving a comment.
Login with GitLab
Email notifications
Send me email when this toolbox has changes, new files or a new release.
You must register and log in before setting up notifications.