repeat

Syntax

repeat(X, n)

Details

Repeats each item in string X n times to form a new string. The size of the result is the same as the size of X.

DolphinDB's repeat and NumPy's numpy.repeat differ in the following aspects:
  • Supported data types: DolphinDB's repeat only supports string scalars or vectors, while NumPy's numpy.repeat supports a wide range of ndarray data types, including numeric and boolean types.

  • Semantic difference:
    • DolphinDB's repeat performs content-level repetition for each string element, making it a string-level transformation; whereas NumPy's numpy.repeat performs element-wise replication of array elements, making it an element-level expansion. The former changes the content of each element, while the latter changes the number of elements. For example, repeat(["ab", "cd"], 2) in DolphinDB returns ["abab","cdcd"], while np.repeat(["ab","cd"], 2) returns ['ab' 'ab' 'cd' 'cd'].

    • NumPy also supports specifying per-element repeat counts (vectorized repetition), e.g., np.repeat(["a","b","c"], [1,2,3]), which is not supported in DolphinDB.
  • Return type difference: DolphinDB returns a STRING scalar or vector, while NumPy returns an ndarray.

Parameters

X is a string scalar/vector.

n is a positive integer.

Examples

repeat(`FB, 3);
// output: FBFBFB

repeat(`AB`CD,2);
// output: ["ABAB","CDCD"]