eraseBetween - Delete substrings between start and end points - MATLAB (original) (raw)
Delete substrings between start and end points
Syntax
Description
[newStr](#bvdoi31-1-newStr) = eraseBetween([str](#bvdoi31-1%5Fsep%5Fbu4l86d-str),[startPat](#bvdoi31-1-startStr),[endPat](#bvdoi31-1-endStr))
deletes all characters from str
that occur between the substringsstartPat
and endPat
, but does not deletestartPat
and endPat
themselves.eraseBetween
returns the remaining text asnewStr
.
If str
is a string array or a cell array of character vectors, then eraseBetween
deletes characters from each element ofstr
. The output argument newStr
has the same data type as str
.
[newStr](#bvdoi31-1-newStr) = eraseBetween([str](#bvdoi31-1%5Fsep%5Fbu4l86d-str),[startPos](#bvdoi31-1-startPos),[endPos](#bvdoi31-1-endPos))
deletes all characters from str
that occur between the positionsstartPos
and endPos
, including the characters at those positions.
[newStr](#bvdoi31-1-newStr) = eraseBetween(___,'Boundaries',[bounds](#mw%5F4cd5e4c9-64ef-48a7-b468-82596dff3883))
forces the starts and ends specified in any of the previous syntaxes to be either inclusive or exclusive. They are inclusive when bounds
is'inclusive'
, and exclusive when bounds
is'exclusive'
. For example,eraseBetween(str,startPat,endPat,'Boundaries','inclusive')
deletes startPat
, endPat
, and all the text between them.
Examples
Delete Text Between Substrings
Create string arrays. Then delete text that occurs between substrings.
str = "The quick brown fox"
str = "The quick brown fox"
Delete the text that occurs between the substrings "quick"
and " fox"
. The eraseBetween
function deletes the text but does not delete "quick"
or " fox"
.
newStr = eraseBetween(str,"quick"," fox")
Delete substrings from each element of a string array. When you specify different substrings as start and end indicators, they must be contained in a string array or a cell array of character vectors that is the same size as str
.
str = ["The quick brown fox jumps";"over the lazy dog"]
str = 2x1 string "The quick brown fox jumps" "over the lazy dog"
startPos = ["quick";"the"]; endPos = [" fox";" dog"]; newStr = eraseBetween(str,startPos,endPos)
newStr = 2x1 string "The quick fox jumps" "over the dog"
Delete Text Between Patterns
Create a string array that has text enclosed by tags.
str = ["Calculus I"; "Fall 2020"; "MWF 8:00-8:50"]
str = 3x1 string "Calculus I" "Fall 2020" "MWF 8:00-8:50"
Delete the text enclosed by tags. First create patterns that match any start tag and end tag by using the wildcardPattern
function.
startPat = "<" + wildcardPattern + ">"
startPat = pattern Matching:
"<" + wildcardPattern + ">"
endPat = "</" + wildcardPattern + ">"
endPat = pattern Matching:
"</" + wildcardPattern + ">"
Then call the eraseBetween
function.
newStr = eraseBetween(str,startPat,endPat)
newStr = 3x1 string "" "" ""
For a list of function that create pattern objects, see pattern.
Delete Substrings Between Start and End Positions
Create string arrays and delete substrings between start and end positions that are specified as numbers.
Delete a substring. To delete the middle name and one of the space characters, specify the sixth and 11th positions in the string. The deleted substring includes the sixth and 11th characters.
newStr = eraseBetween(str,6,11)
Delete substrings from each element of a string array. When you specify different start and end positions with numeric arrays, they must be the same size as the input string array.
str = ["Edgar Allen Poe";"Louisa May Alcott"]
str = 2x1 string "Edgar Allen Poe" "Louisa May Alcott"
startsPos = [6;7]; endPos = [11;10]; newStr = eraseBetween(str,startsPos,endPos)
newStr = 2x1 string "Edgar Poe" "Louisa Alcott"
Delete Text with Inclusive and Exclusive Boundaries
Delete text from string arrays with boundaries that are forced to be inclusive or exclusive. eraseBetween
deletes the boundaries when they are inclusive. eraseBetween
returns the boundaries as part of the output string array when they are exclusive.
str = "small|medium|large"
str = "small|medium|large"
Delete the text between sixth and 13th positions, but do not delete the characters at those positions.
newStr = eraseBetween(str,6,13,'Boundaries','exclusive')
Delete the text between two substrings, and also the substrings themselves.
str = "The quick brown fox jumps over the lazy dog"
str = "The quick brown fox jumps over the lazy dog"
newStr = eraseBetween(str," brown","lazy",'Boundaries','inclusive')
Delete Text Between Positions in Character Vector
Create a character vector and delete text between start and end positions.
chr = 'mushrooms, peppers, and onions'
chr = 'mushrooms, peppers, and onions'
newChr = eraseBetween(chr,10,19)
newChr = 'mushrooms and onions'
Delete text between substrings.
newChr = eraseBetween(chr,'mushrooms',' and')
newChr = 'mushrooms 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.
startPat
— Text or pattern that marks start position
string array | character vector | cell array of character vectors | pattern
array
Text or pattern that marks the start position of the text to delete, specified as one of the following:
- String array
- Character vector
- Cell array of character vectors
- pattern array
If str
is a string array or cell array of character vectors, then you can delete substrings from every element ofstr
. You can specify that the substrings either all have the same start or have different starts in each element ofstr
.
- To specify the same start, specify
startPat
as a character vector, string scalar, orpattern
object. - To specify different starts, specify
startPat
as a string array, cell array of character vectors, orpattern
array.
Example: eraseBetween(str,"AB","YZ")
deletes all characters between AB
and YZ
in each element of str
.
Example: If str
is a2
-by-1
string array, theneraseBetween(str,["AB";"FG"],["YZ";"ST"])
deletes all characters between AB
and YZ
instr(1)
, and between FG
andST
in str(2)
.
endPat
— Text or pattern that marks end position
string array | character vector | cell array of character vectors | pattern
array
Text or pattern that marks the end position of the text to delete, specified as one of the following:
- String array
- Character vector
- Cell array of character vectors
- pattern array
If str
is a string array or cell array of character vectors, then you can delete substrings from every element ofstr
. You can specify that the substrings either all have the same end or have different ends in each element ofstr
.
- To specify the same end, specify
endPat
as a character vector, string scalar, orpattern
object. - To specify different ends, specify
endPat
as a string array, cell array of character vectors, orpattern
array.
Example: eraseBetween(str,"AB","YZ")
deletes all characters between AB
and YZ
in each element of str
.
Example: If str
is a2
-by-1
string array, theneraseBetween(str,["AB";"FG"],["YZ";"ST"])
deletes all characters between AB
and YZ
instr(1)
, and between FG
andST
in str(2)
.
startPos
— Start position
numeric array
Start position, specified as a numeric array.
If str
is a string array or cell array of character vectors, then startPos
can be a numeric scalar or numeric array of the same size as str
.
Example: eraseBetween(str,5,9)
deletes all characters from the fifth through the ninth positions in each element ofstr
.
Example: If str
is a2
-by-1
string array, theneraseBetween(str,[5;10],[9;21])
deletes all characters from the fifth through the ninth positions instr(1)
, and from the 10th through the 21st positions in str(2)
.
endPos
— End position
numeric array
End position, specified as a numeric array.
If str
is a string array or cell array of character vectors, then endPos
can be a numeric scalar or numeric array of the same size as str
.
Example: eraseBetween(str,5,9)
deletes all characters from the fifth through the ninth positions in each element ofstr
.
Example: If str
is a2
-by-1
string array, theneraseBetween(str,[5;10],[9;21])
deletes all characters from the fifth through the ninth positions instr(1)
, and from the 10th through the 21st positions in str(2)
.
bounds
— Boundary behavior
'inclusive'
| 'exclusive'
Boundary behavior, specified as 'inclusive'
or'exclusive'
. When boundary behavior is inclusive the start and end specified by previous arguments are included in the deleted text. If boundary behavior is exclusive, then the start and end are not included.
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.
TheeraseBetween
function fully supports tall arrays. For more information, see Tall Arrays.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
str
,startPat
, andendPat
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™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced in R2016b