extractBefore

Extract substring before specified position

Syntax

  • newStr = extractBefore(str,endStr)
    example
  • newStr = extractBefore(str,end)
    example

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 an array that contains multiple pieces of text, then extractBefore extracts substrings from each element of str. The output argument newStr is the same size as str.

example

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

Examples

collapse all

Create string arrays and select text that occurs before substrings.

Create a string. Then create a new string that occurs before the substring ' brown'. The extractBefore function selects the text but does not include ' brown' in the output.

str = string('The quick brown fox')
str = 

  string

    "The quick brown fox"

newStr = extractBefore(str,' brown')
newStr = 

  string

    "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 = string({'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.

Create a string that contains a name.

str = string('Edgar Allen Poe')
str = 

  string

    "Edgar Allen Poe"

Select the substring before the sixth character.

newStr = extractBefore(str,6)
newStr = 

  string

    "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 = string({'Edgar Allen Poe';'Louisa May Alcott'})
newStr = extractBefore(str,[6;7])
str = 

  2×1 string array

    "Edgar Allen Poe"
    "Louisa May Alcott"


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 = 

  string

    "peppers"

Select text before a substring.

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

  string

    "peppers"

Related Examples

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 an array with multiple pieces of text, 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 an array with multiple pieces of text, then end 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. newStr is always a string array, even when str is a character vector or a cell array of character vectors.

Data Types: string

More About

collapse all

Tall Array Support

This function fully supports tall arrays. For more information, see Tall Arrays.

Introduced in R2016b

Was this topic helpful?