insertAfter
Insert string after specified substring
Syntax
newStr = insertAfter(str,startStr,newText)
newStr = insertAfter(str,startPos,newText)
Description
insertsnewStr
= insertAfter(str
,startStr
,newText
)newText
intostr
after the substring specified bystartStr
and returns the result asnewStr
. IfstartStr
occurs multiple times instr
, theninsertAfter
inserts text after every occurrence ofstartStr
.
Ifstr
是一个字符串array or a cell array of character vectors, theninsertAfter
insertsnewText
into each element ofstr
. The output argumentnewStr
has the same data type asstr
.
Examples
Insert Text After Substring
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"
Insert Substring After Position
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"
Insert Text After Position in Character Vector
创建一个特征向量和之后插入文本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
str
—Input text
string array|character vector|cell array of character vectors
Input text, specified as a string array, a character vector, or a cell array of character vectors.
Data Types:string
|char
|cell
startStr
—Substring to insert text after
string array|character vector|cell array of character vectors
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, thenstartStr
can 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
startPos
—Start position to insert text after
numeric array
Start position to insert text after, specified as a numeric array.
Ifstr
是一个字符串array or a cell array of character vectors, thenstartPos
can 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
newText
—Text to insert
string array|character vector|cell array of character vectors
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, thennewText
can 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
newStr
— Output text
string array | character vector | cell array of character vectors
Output text, returned as a string array, a character vector, or a cell array of character vectors.str
andnewStr
have the same data type.
Data Types:string
|char
|cell
Extended Capabilities
Tall Arrays
Calculate with arrays that have more rows than fit in memory.
This function fully supports tall arrays. For more information, seeTall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
,startStr
, andnewText
must be a string scalar, a character vector, or a cell array containing not more than one character vector.
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, seeRun MATLAB Functions with Distributed Arrays(Parallel Computing Toolbox).
Introduced in R2016b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)