Delete substrings within strings - MATLAB erase (2024)

Delete substrings within strings

collapse all in page

Syntax

newStr = erase(str,match)

Description

example

newStr = erase(str,match) deletes all occurrences of match in str. The erase function returns the remaining text as newStr.

If match is an array, then erase deletes every occurrence of every element of match in str. The str and match arguments do not need to be the same size.

Examples

collapse all

Delete Substrings from String Array

Open Live Script

Create a string array and delete substrings from it.

str = ["the quick brown fox jumps"; "over the lazy dog"]
str = 2x1 string "the quick brown fox jumps" "over the lazy dog"

Delete the substring "the " from str. The erase function deletes both instances.

newStr = erase(str,"the ")

Delete multiple substrings from str.

match = ["the ","quick ","lazy "];newStr = erase(str,match)
newStr = 2x1 string "brown fox jumps" "over dog"

Delete File Paths Using Pattern

Open Live Script

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"

Delete the paths, leaving only file names. To match paths, create a pattern using the wildcardPattern function that matches all text that includes a final "\" character. Use that pattern with the erase function.

match = wildcardPattern + "\"
match = pattern Matching: wildcardPattern + "\"
filenames = erase(str,match)
filenames = 3x1 string "MyReport.docx" "Sample1.csv" "Slides.pptx"

For a list of functions that create pattern objects, see pattern.

Delete Substring from Character Vector

Create a character vector. Delete the substring, ' World', including the space character.

chr = 'Hello World'
chr = 'Hello World'
newChr = erase(chr,' World')
newChr = 'Hello'

Input Arguments

collapse all

strInput 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.

matchText to delete
string array | character vector | cell array of character vectors | pattern array

Text to delete, specified as one of the following:

  • String array

  • Character vector

  • Cell array of character vectors

  • pattern array

Tips

  • To delete multiple occurrences of a match when theoccurrences overlap, use the strrep function. erase onlydeletes the first occurrence when occurrences overlap.

Extended Capabilities

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.

Delete substrings within strings - MATLAB erase (1)

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:

Americas

Europe

Asia Pacific

Contact your local office

Delete substrings within strings - MATLAB erase (2024)

FAQs

Delete substrings within strings - MATLAB erase? ›

newStr = erase( str , substr ) deletes instances of the substring substr that occur in the string str . The erase operator is not supported in Stateflow

Stateflow
Stateflow is a product that provides a graphical language that includes state transition diagrams, flow charts, state transition tables, and truth tables. You can use Stateflow to describe how MATLAB algorithms and Simulink models react to input signals, events, and time-based conditions.
https://www.mathworks.com › products › stateflow
® charts that use C as the action language.

How to remove substring from string? ›

To remove all instances of a substring, employ the replaceAll() method. It functions like replace() , but affects every occurrence of the specified string.

How to remove certain characters from a string in Matlab? ›

newStr = erase( str , match ) deletes all occurrences of match in str . The erase function returns the remaining text as newStr .

How to trim a string in Matlab? ›

newStr = strtrim( str ) removes the leading and trailing whitespace characters from the string str . Use this operator in the Requirements Table block.

How to extract substring from string in matlab? ›

newStr = substr( str , pos , length ) returns the substring of str that starts at the character position pos and is length characters long. Use zero-based indexing. The operator substr is supported only in Stateflow® charts that use C as the action language.

Can you use remove() on strings? ›

remove() The String remove() function modifies a string, in place, removing chars from the provided index to the end of the string or from the provided index to index plus count.

How do I extract a substring from a string? ›

You call the Substring(Int32) method to extract a substring from a string that begins at a specified character position and ends at the end of the string.

How do you erase a substring from a string in Matlab? ›

newStr = erase( str , substr ) deletes instances of the substring substr that occur in the string str .

How do I remove unwanted characters from a string? ›

Using 'str.

replace(), we can replace a specific character. If we want to remove that specific character, we can replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How to replace substring in Matlab? ›

newStr = strrep( str , old , new ) replaces all occurrences of old in str with new . If any input argument is a nonscalar string array or cell array of character vectors, then the other input arguments must have compatible sizes.

How to check string for substring in matlab? ›

tf = contains( str , substr ) returns 1 ( true ) if the string str contains the substring substr , and returns 0 ( false ) otherwise. tf = contains( str , substr ,IgnoreCase=true) checks if str contains substr , ignoring any differences in letter case.

What does trim () do to a string? ›

The Trim method removes from the current string all leading and trailing white-space characters. Each leading and trailing trim operation stops when a non-white-space character is encountered.

What is the trim function in Matlab? ›

trim can find trim points that meet specific input, output, or state conditions, and it can find points where a system is changing in a specified manner, that is, points where the system's state derivatives equal specific nonzero values.

How do you break a string into a substring? ›

Use the Split method when the substrings you want are separated by a known delimiting character (or characters). Regular expressions are useful when the string conforms to a fixed pattern. Use the IndexOf and Substring methods in conjunction when you don't want to extract all of the substrings in a string.

How does substring () inside string works? ›

The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string. If start is greater than end, arguments are swapped: (4, 1) = (1, 4). Start or end values less than 0, are treated as 0.

How do I extract a substring from a string in go? ›

One of the simplest ways to extract a substring in Go is by using slicing. Go's strings are essentially read-only slices of bytes, making slicing a natural and efficient operation. In this example, originalString[7:13] extracts the substring starting from index 7 up to, but not including, index 13.

How to remove a substring from a string in SQL? ›

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.

How do you remove substrings from a string in Java? ›

The start and end of the substring to be deleted from the string are specified as the first and second int parameters of the delete() function. The last index is exclusive since it subtracts one from the second parameter, but the start index is inclusive. Syntax: public StringBuilder delete(int start,int end)

How do I remove a specific word from a string? ›

Remove Characters From a String Using the replace() Method. The String replace() method replaces a character with a new character. You can remove a character from a string by providing the character(s) to replace as the first argument and an empty string as the second argument.

Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 5852

Rating: 4.8 / 5 (48 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.