insertBefore

Insert strings before specified substrings

Description

example

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

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

example

newStr = insertBefore(str,endPos,newText) inserts the text specified by newText into str before the position specified by endPos.

Examples

collapse all

Create string arrays and insert text before substrings.

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

str = "bread cheese wine"
str = 
"bread cheese wine"

Insert a comma before each space character in the string. The insertBefore function inserts text before each matching substring.

newStr = insertBefore(str," ",",")
newStr = 
"bread, cheese, wine"

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 = insertBefore(str,[" fox";" dog"],[" 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 before the seventh character.

newStr = insertBefore(str,7,"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 = insertBefore(str,[7;6],["Clerk ";"Friedrich "])
newStr = 2x1 string
    "James Clerk Maxwell"
    "Carl Friedrich Gauss"

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

chr = 'mushrooms and onions'
chr = 
'mushrooms and onions'

Insert text before the tenth position.

newChr = insertBefore(chr,10,', peppers,')
newChr = 
'mushrooms, peppers, and onions'

Insert text before a substring.

newChr = insertBefore(chr,' and',', 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 before, 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 endStr 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

End position to insert text before, specified as a numeric array.

If str is a string array or a cell array of character vectors, then endPos 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