Documentation

insertAfter

Insert string after specified substring

Syntax

newStr = insertAfter(str,startStr,newText)
newStr = insertAfter(str,startPos,newText)

Description

example

newStr= insertAfter(str,startStr,newText)insertsnewTextintostrafter the substring specified bystartStrand returns the result asnewStr. IfstartStroccurs multiple times instr, theninsertAfterinserts text after every occurrence ofstartStr.

Ifstr是一个字符串array or a cell array of character vectors, theninsertAfterinsertsnewTextinto each element ofstr. The output argumentnewStrhas the same data type asstr.

example

newStr= insertAfter(str,startPos,newText)inserts the text specified bynewTextintostrafter the position specified bystartPos.

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 asstr.

str = ["The quick fox jumps";"over the dog"]
str =2x1 string array"The quick fox jumps" "over the dog"
newStr = insertAfter(str,["quick";"the"],[" brown";" lazy"])
newStr =2x1 string array"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 array"James Maxwell" "Carl Gauss"
newStr = insertAfter(str,[5;4],[" Clerk";" Friedrich"])
newStr =2x1 string array"James Clerk Maxwell" "Carl Friedrich Gauss"

创建一个特征向量和之后插入文本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.

Ifstr是一个字符串array or a cell array of character vectors, thenstartStrcan be a character vector, a string scalar, or a string array or a cell array of the same size asstr.

Data Types:string|char|cell

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

Ifstr是一个字符串array or a cell array of character vectors, thenstartPoscan be a numeric scalar or a numeric array of the same size asstr.

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.

Ifstr是一个字符串array or a cell array of character vectors, thennewTextcan be a character vector, a string scalar, or a string array or a cell array of the same size asstr.

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.strandnewStrhave the same data type.

Data Types:string|char|cell

Extended Capabilities

Introduced in R2016b