Documentation

eraseBetween

Delete substrings between indicators that mark starts and ends of substrings

Syntax

newStr = eraseBetween(str,startStr,endStr)
newStr = eraseBetween(str,startPos,endPos)
newStr = eraseBetween(___,'Boundaries',bounds)

Description

example

newStr= eraseBetween(圣r,圣artStr,endStr)deletes all characters from圣rthat occur between the substrings圣artStrandendStr,但not delete圣artStrandendStrthemselves.eraseBetweenreturns the remaining text asnewStr.

If圣ris a string array or a cell array of character vectors, theneraseBetweendeletes characters from each element of圣r. The output argumentnewStrhas the same data type as圣r.

example

newStr= eraseBetween(圣r,圣artPos,endPos)deletes all characters from圣rthat occur between the positions圣artPosandendPos, including the characters at those positions.

example

newStr= eraseBetween(___,'Boundaries',bounds)forces the starts and ends specified in any of the previous syntaxes to be either inclusive or exclusive. They are inclusive whenboundsis'inclusive', and exclusive whenboundsis'exclusive'. For example,eraseBetween(str,startStr,endStr,'Boundaries','inclusive')deletes圣artStr,endStr, and all the text between them.

Examples

collapse all

Create string arrays. Then delete text that occurs between substrings.

Create a string. Starting in R2017a, you can create strings using double quotes instead of the圣ringfunction.

圣r ="The quick brown fox"
圣r = "The quick brown fox"

Delete the text that occurs between the substrings"quick"and" fox". TheeraseBetweenfunction deletes the text but does not delete"quick"or" fox".

newStr = eraseBetween(str,"quick"," fox")
newStr = "The 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圣r.

圣r = ["The quick brown fox jumps";"over the lazy dog"]
圣r =2×1 string array"The quick brown fox jumps" "over the lazy dog"
圣artPos = ["quick";"the"]; endPos = [" fox";" dog"]; newStr = eraseBetween(str,startPos,endPos)
newStr =2×1 string array"The quick fox jumps" "over the dog"

Create string arrays and delete substrings between start and end positions that are specified as numbers.

Create a string that contains a name. Starting in R2017a, you can create strings using double quotes instead of the圣ringfunction.

圣r ="Edgar Allen Poe"
圣r = "Edgar Allen Poe"

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 incldues the sixth and 11th characters.

newStr = eraseBetween(str,6,11)
newStr = "Edgar Poe"

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.

圣r = ["Edgar Allen Poe";"Louisa May Alcott"]
圣r =2×1 string array"Edgar Allen Poe" "Louisa May Alcott"
圣artsPos = [6;7]; endPos = [11;10]; newStr = eraseBetween(str,startsPos,endPos)
newStr =2×1 string array"Edgar Poe" "Louisa Alcott"

Delete text from string arrays with boundaries that are forced to be inclusive or exclusive.eraseBetweendeletes the boundaries when they are inclusive.eraseBetweenreturns the boundaries as part of the output string array when they are exclusive.

Create a string array. Starting in R2017a, you can create strings using double quotes instead of the圣ringfunction.

圣r ="small|medium|large"
圣r = "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')
newStr = "small||large"

Delete the text between two substrings, and also the substrings themselves.

圣r ="The quick brown fox jumps over the lazy dog"
圣r = "The quick brown fox jumps over the lazy dog"
newStr = eraseBetween(str," brown","lazy",'Boundaries','inclusive')
newStr = "快速狗”

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

collapse all

Input text, specified as a string array, a character vector, or a cell array of character vectors.

Data Types:圣ring|char|cell

String that indicates the start of the substring to delete, specified as a string array, a character vector, or a cell array of character vectors.

If圣ris a string array or a cell array of character vectors, then圣artStrcan be a character vector, a string scalar, or a string array or a cell array of the same size as圣r.

Example:eraseBetween(str,"AB","YZ")deletes all characters betweenABandYZin each element of圣r.

Example:If圣ris a2-by-1圣ring array, theneraseBetween(str,["AB";"FG"],["YZ";"ST"])deletes all characters betweenABandYZin圣r(1), and betweenFGandSTin圣r(2).

Data Types:圣ring|char|cell

String that indicates the end of the substring to delete, specified as a string array, a character vector, or a cell array of character vectors.

If圣ris a string array or a cell array of character vectors, thenendStrcan be a character vector, a string scalar, or a string array or a cell array of the same size as圣r.

Example:eraseBetween(str,"AB","YZ")deletes all characters betweenABandYZin each element of圣r.

Example:If圣ris a2-by-1圣ring array, theneraseBetween(str,["AB";"FG"],["YZ";"ST"])deletes all characters betweenABandYZin圣r(1), and betweenFGandSTin圣r(2).

Data Types:圣ring|char|cell

Start position of substring to delete, specified as a numeric array.

If圣ris a string array or a cell array of character vectors, then圣artPoscan be a numeric scalar or a numeric array of the same size as圣r.

Example:eraseBetween(str,5,9)deletes all characters from the fifth through the ninth positions in each element of圣r.

Example:If圣ris a2-by-1圣ring array, theneraseBetween(str,[5;10],[9;21])deletes all characters from the fifth through the ninth positions in圣r(1), and from the 10th through the 21st positions in圣r(2).

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

End position of substring to delete, specified as a numeric array.

If圣ris a string array or a cell array of character vectors, thenendPoscan be a numeric scalar or a numeric array of the same size as圣r.

Example:eraseBetween(str,5,9)deletes all characters from the fifth through the ninth positions in each element of圣r.

Example:If圣ris a2-by-1圣ring array, theneraseBetween(str,[5;10],[9;21])deletes all characters from the fifth through the ninth positions in圣r(1), and from the 10th through the 21st positions in圣r(2).

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Output Arguments

collapse all

Output text, returned as a string array, a character vector, or a cell array of character vectors.圣randnewStrhave the same data type.

Data Types:圣ring|char|cell

Extended Capabilities

Introduced in R2016b

Was this topic helpful?