insertAfter

Insert strings after specified substrings

Description

example

newStr = insertAfter(str,startStr,newText) inserts newText into str after the substring specified by startStr and returns the result as newStr. If startStr occurs multiple times in str, then insertAfter inserts text after every occurrence of startStr.

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 = insertAfter(str,startPos,newText) inserts the text specified by newText into str after the position specified by startPos.

Examples

collapse all

Create string arrays and insert text after substrings.

Starting in R2017a, you can create strings using double quotes.

str = "The quick fox"
str = 
"The quick fox"

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"

Create string arrays and specify positions to insert substrings.

Starting in R2017a, you can create strings using double quotes.

str = "James Maxwell"
str = 
"James Maxwell"

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"

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

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

Data Types: string | char | cell

Substring to insert text after, specified as a string array, a character vector, or a cell array of character vectors.

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

Data Types: string | char | cell

Start position to insert text after, specified as a numeric array.

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

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

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

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

Data Types: string | char | cell

Output Arguments

collapse all

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

Data Types: string | char | cell

Extended Capabilities

Introduced in R2016b