strrepn

by Tim C. Lueth, SG-Lib Toolbox: SolidGeometry 5.6 - Text/Strs/Chars/Links
Introduced first in SolidGeometry 3.2, Creation date: 2017-01-02, Last change: 2025-09-14

replaces several string elements by using strrep

Description

extension of strrep, with more than 1 pair of parameters. The exchange takes place in order of the pairs.
Since SG-Lib 5.1 oldSubStr and newSubstr can be a cell list

See Also: , strfindafter , strfindtag , strrepalias

Example Illustration

 missing image of strrepn(origStr,oldSubStr,newSubstr)

Syntax

S=strrepn(origStr,[oldSubStr,newSubstr])

Input Parameter

origStr: Original String
oldSubStr: Substring to exchange
newSubstr: New Substring

Output Parameter

S: Resulting string

Examples


strrepn('abcdef','a','x','d','y')
strrepn('abcdef',{'a','d'},{'x','y'})




Copyright 2017-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth

Algorithm (Workflow)

This algorithm is an extension of the MATLAB function strrep, designed to replace multiple substrings within a given string. It allows for more than one pair of old and new substrings to be specified, and the replacements occur in the order the pairs are provided.

Input Parameters

Output

Algorithm Steps

  1. Check if origStr is empty. If it is, return an empty string S.
  2. Determine if the first two elements of varargin are cell arrays. If they are:
    • Initialize S with origStr.
    • Assign Osub to the first cell array and Nsub to the second cell array.
    • Iterate over each element in Osub and Nsub, replacing occurrences of each Osub element in S with the corresponding Nsub element using strrep.
    • Return the modified string S.
  3. If the inputs are not cell arrays:
    • Calculate n as half the number of additional arguments, indicating the number of old-new substring pairs.
    • Initialize S with origStr.
    • Iterate over each pair of old and new substrings:
      • Use strrep to replace each old substring with the corresponding new substring in S.

Example Usage

Algorithm explaination created using ChatGPT on 2025-08-19 06:47. (Please note: No guarantee for the correctness of this explanation)

Last html export of this page out of FM database by TL: 2025-09-21