insertBefore
Insert strings before specified substrings
Description
inserts newStr
= insertBefore(str
,pat
,newText
)newText
into str
before the substring
specified by pat
and returns the result as
newStr
. If pat
occurs multiple times in
str
, then insertBefore
inserts text before
every occurrence of pat
.
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
.
Examples
Insert Text Before Substring
Create string arrays and insert text before substrings.
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"
Insert Text Before File Names Using Pattern
Since R2020b
Create a string array of file names, including full paths.
str = ["C:\Temp\MyReport.docx"; "C:\Data\Experiment1\Trial1\Sample1.csv"; "C:\Temp\Slides.pptx"]
str = 3x1 string
"C:\Temp\MyReport.docx"
"C:\Data\Experiment1\Trial1\Sample1.csv"
"C:\Temp\Slides.pptx"
Insert a new folder name at the end of each path, just before the file name. To match the file names, create a pattern that matches the last "\"
character and all remaining text to the end of a string. Use the wildcardPattern
function to match all characters except "\"
and the textBoundary
function to match the end of the string. Then call insertBefore
to insert the new folder name before the matching text.
pat = "\" + wildcardPattern("Except","\") + textBoundary
pat = pattern
Matching:
"\" + wildcardPattern("Except","\") + textBoundary
filenames = insertBefore(str,pat,"\20200601")
filenames = 3x1 string
"C:\Temp\20200601\MyReport.docx"
"C:\Data\Experiment1\Trial1\20200601\Sample1.csv"
"C:\Temp\20200601\Slides.pptx"
For a list of functions that create pattern objects, see pattern
.
Insert Substring Before Position
Create string arrays and specify positions to insert substrings.
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"
Insert Text Before Position in Character Vector
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
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 end position
string array | character vector | cell array of character vectors | pattern
array (since R2020b)
Text or pattern in str
that marks the end position for
inserted text, specified as one of the following:
String array
Character vector
Cell array of character vectors
pattern
array (since R2020b)
If str
is a string array or cell array of character
vectors, then you can insert text into every element of
str
. You can specify that the insertions either all
have the same end or have different ends in each element of
str
.
To specify the same end, specify
pat
as a character vector, string scalar, orpattern
object.To specify different ends, specify
pat
as a string array, cell array of character vectors, orpattern
array.
pos
— End position
numeric array
End position, specified as a numeric array.
If str
is a string array or 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 as
str
.
Output Arguments
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.
The
insertBefore
function supports tall arrays with the following usage
notes and limitations:
If
pat
is an array of pattern objects, the size of the first dimension of the array must be 1.
For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
,pat
, andnewText
must be a string scalar, a character vector, or a cell array containing not more than one character vector.
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:
pattern
objects must be scalar.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)