insertAfter - Insert strings after specified substrings - MATLAB (original) (raw)

Insert strings after specified substrings

Syntax

Description

[newStr](#bvdtpjr-1-newStr) = insertAfter([str](#bvdtpjr-1%5Fsep%5Fbu4l86d-str),[pat](#bvdtpjr-1-startStr),[newText](#bvdtpjr-1-newText)) inserts newText into str after the substring specified by pat and returns the result asnewStr. If pat occurs multiple times instr, then insertAfter inserts text after every occurrence of pat.

If str is a string array or a cell array of character vectors, then insertAfter inserts newText into each element of str. The output argument newStr has the same data type as str.

example

[newStr](#bvdtpjr-1-newStr) = insertAfter([str](#bvdtpjr-1%5Fsep%5Fbu4l86d-str),[pos](#bvdtpjr-1-startPos),[newText](#bvdtpjr-1-newText)) inserts the text specified by newText into str after the position specified by pos.

example

Examples

collapse all

Insert Text After Substring

Create string arrays and insert text after substrings.

You can create strings using double quotes.

Insert text after the substring "quick".

newStr = insertAfter(str,"quick"," brown")

newStr = "The quick brown fox"

Insert substrings into each element of a string array. When you specify different substrings as positions, they must be contained in a string array or a cell array that is the same size as str.

str = ["The quick fox jumps";"over the dog"]

str = 2x1 string "The quick fox jumps" "over the dog"

newStr = insertAfter(str,["quick";"the"],[" brown";" lazy"])

newStr = 2x1 string "The quick brown fox jumps" "over the lazy dog"

Insert Text After Start of Path Using Pattern

Since R2020b

Create a string array of file names, including full paths. The paths begin with different drive letters.

str = ["C:\Temp\MyReport.docx"; "D:\Data\Experiment1\Trial1\Sample1.csv"; "E:\Presentations\Slides.pptx"]

str = 3x1 string "C:\Temp\MyReport.docx" "D:\Data\Experiment1\Trial1\Sample1.csv" "E:\Presentations\Slides.pptx"

Insert a new folder name after each drive letter. To match the drive letters, create a pattern that matches a single letter using the lettersPattern function, and follows it with the characters ":\". Then call insertAfter.

pat = lettersPattern(1) + ":"

pat = pattern Matching:

lettersPattern(1) + ":\"

filenames = insertAfter(str,pat,"Archive")

filenames = 3x1 string "C:\Archive\Temp\MyReport.docx" "D:\Archive\Data\Experiment1\Trial1\Sample1.csv" "E:\Archive\Presentations\Slides.pptx"

For a list of functions that create pattern objects, see pattern.

Insert Substring After Position

Create string arrays and specify positions to insert substrings.

You can create strings using double quotes.

Insert a substring after the fifth character.

newStr = insertAfter(str,5," Clerk")

newStr = "James Clerk Maxwell"

Insert substrings into each element of a string array. When you specify different positions with numeric arrays, they must be the same size as the input string array.

str = ["James Maxwell";"Carl Gauss"]

str = 2x1 string "James Maxwell" "Carl Gauss"

newStr = insertAfter(str,[5;4],[" Clerk";" Friedrich"])

newStr = 2x1 string "James Clerk Maxwell" "Carl Friedrich Gauss"

Insert Text After Position in Character Vector

Create a character vector and insert text after a specified position.

chr = 'mushrooms and onions'

chr = 'mushrooms and onions'

Insert text after the ninth position.

newChr = insertAfter(chr,9,', peppers,')

newChr = 'mushrooms, peppers, and onions'

Insert text after a substring.

newChr = insertAfter(chr,'mushrooms',', peppers,')

newChr = 'mushrooms, peppers, and onions'

Input Arguments

collapse all

str — Input text

string array | character vector | cell array of character vectors

Input text, specified as a string array, character vector, or cell array of character vectors.

pat — Text or pattern that marks start position

string array | character vector | cell array of character vectors | pattern array (since R2020b)

Text or pattern in str that marks the start position for inserted text, specified as one of the following:

If str is a string array or cell array of character vectors, then you can insert text into every element ofstr. You can specify that the insertions either all have the same start or have different starts in each element ofstr.

pos — Start position

numeric array

Start position, specified as a numeric array.

If str is a string array or a cell array of character vectors, then pos can be a numeric scalar or numeric array of the same size as str.

newText — Text to insert

string array | character vector | cell array of character vectors

Text to insert, specified as a string array, character vector, or cell array of character vectors.

If str is a string array or cell array of character vectors, then newText can be a character vector, string scalar, or a string array or cell array of the same size asstr.

Output Arguments

collapse all

newStr — Output text

string array | character vector | cell array of character vectors

Output text, returned as a string array, character vector, or cell array of character vectors. str and newStr have the same data type.

Extended Capabilities

Tall Arrays

Calculate with arrays that have more rows than fit in memory.

TheinsertAfter function supports tall arrays with the following usage notes and limitations:

For more information, see Tall Arrays.

C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Thread-Based Environment

Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.

Distributed Arrays

Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced in R2016b