Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

extractBefore

Extract substring before specified position

Syntax

newStr = extractBefore(str,endStr)
newStr = extractBefore(str,endPos)

Description

example

newStr = extractBefore(str,endStr) extracts the substring that begins with the first character of str and ends before endStr. If endStr occurs multiple times in str, then newStr is str from the start of str up to the first occurrence of endStr

If str is a string array or a cell array of character vectors, then extractBefore extracts substrings from each element of str. The output argument newStr has the same data type as str.

example

newStr = extractBefore(str,endPos) extracts the substring that begins with the first character of str and ends before the position specified by endPos.

Examples

collapse all

Create string arrays and select text that occurs before substrings.

Starting in R2017a, you can create strings using double quotes instead of the string function.

str = "The quick brown fox"
str = 
"The quick brown fox"

Extract the substring that occurs before the substring " brown". The extractBefore function selects the text but does not include " brown" in the output.

newStr = extractBefore(str," brown")
newStr = 
"The quick"

Create a new string array from the elements 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 brown fox jumps";"over the lazy dog"]
str = 2×1 string array
    "The quick brown fox jumps"
    "over the lazy dog"

newStr = extractBefore(str,[" brown";" dog"])
newStr = 2×1 string array
    "The quick"
    "over the lazy"

You also can specify one substring as a position that is applied to all elements of the input string array.

Create strings before specified positions.

Starting in R2017a, you can create strings using double quotes instead of the string function.

str = "Edgar Allen Poe"
str = 
"Edgar Allen Poe"

Select the substring before the sixth character.

newStr = extractBefore(str,6)
newStr = 
"Edgar"

Select substrings from 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 = ["Edgar Allen Poe";"Louisa May Alcott"]
str = 2×1 string array
    "Edgar Allen Poe"
    "Louisa May Alcott"

newStr = extractBefore(str,[6;7])
newStr = 2×1 string array
    "Edgar"
    "Louisa"

Select substrings from each element and specify the same position.

newStr = extractBefore(str,12)
newStr = 2×1 string array
    "Edgar Allen"
    "Louisa May "

Create a character vector. Then create new character vectors that are substrings of chr.

chr = 'peppers and onions'
chr = 
'peppers and onions'

Select the substring before the eighth position.

newChr = extractBefore(chr,8)
newChr = 
'peppers'

Select text before a substring.

newChr = extractBefore(chr,' and')
newChr = 
'peppers'

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

String that indicates end of substring to extract, specified as a string array, a character vector, or a cell array of character vectors. extractBefore excludes endStr from the substring to extract.

If str is a string array or 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 of substring to extract, 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 a numeric array of the same size as str.

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.

Data Types: string | char | cell

Extended Capabilities

Introduced in R2016b

Was this topic helpful?