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
Syntax
S=strrepn(origStr,[oldSubStr,newSubstr])
Input Parameter
origStr: | | Original String |
oldSubStr: | | Substring to exchange |
newSubstr: | | New Substring |
Output Parameter
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
- origStr: The original string in which replacements are to be made.
- varargin: A variable-length input argument list containing pairs of old and new substrings. These can be provided as individual strings or as cell arrays.
Output
- S: The resulting string after all specified replacements have been made.
Algorithm Steps
- Check if
origStr is empty. If it is, return an empty string S.
- 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.
- 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
strrepn('abcdef','a','x','d','y'): Replaces 'a' with 'x' and 'd' with 'y' in the string 'abcdef'.
strrepn('abcdef',{'a','d'},{'x','y'}): Uses cell arrays to achieve the same replacements as the previous example.
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