Strings are the building blocks of all programming languages and they are widely used to perform many tasks. MATLAB is a high-performance programming language and it allows us to perform many string operations. One such operation is removing spaces in a string which can be easily performed using various MATLAB built-in functions.

If you are not familiar with how to remove spaces in a string in MATLAB, follow this blog to address this query.

## How to Remove Spaces from a String in MATLAB?

Removing spaces in a string can be easily done by implementing various MATLAB’s built-in functions that are given below:

- Using strtrim() function
- Using erase() function
- Using deblank() function
- Using regexprep() function
- Using strrep() function

## 1: How to Eliminate Spaces from a String Using the strtrim() Function?

The **strtrim() **is a built-in MATLAB function that allows us to remove leading and trailing whitespaces in a string. This function takes a string as an input argument and provides a new string having no leading and trailing spaces.

**Syntax**

The **strtrim()** function’s syntax is given below:

**Example**

In this example, we remove the leading and trailing spaces from the given string str using the **strtrim() **function in MATLAB.

new_Str = strtrim(Str)

## 2: How to Remove Spaces in a String Using the erase() Function?

The **erase()** is MATLAB’s built-in function that is used for removing all occurrences of the given match. This function can be used for removing spaces from a string by specifying space **“ ” **as a match. This function accepts a string and a match as arguments and returns a manipulated string having no match.

**Syntax**

The **erase()** function’s syntax is given below:

**Example**

This example removes all spaces from the given string str using the **erase()** function in MATLAB.

new_Str = erase(Str, " ")

## 3: How to Remove Spaces in a String Using the deblank() Function?

The **deblank()** is a built-in function in MATLAB that enables us to remove the trailing white spaces and null characters from the given string. This function takes a string as an input argument and provides a modified string having no trailing white spaces and a null character.

**Note: **It is important to note that the **deblank() **function doesn’t remove the whitespaces that appear at the beginning of a string.

**Syntax**

The **deblank()** function’s syntax is given below:

**Example**

In this MATLAB code, we remove the trailing spaces from the given string str using the **deblank()** function in MATLAB.

new_Str = deblank(Str)

## 4: How to Remove Spaces in a String Using the regexprep() Function?

The **regexprep()** is a built-in MATLAB function that allows us to manipulate a string using the given regular expression. This function accepts a string, a regular expression, and a replaced text as arguments and returns a new modified string with no spaces.

**Syntax**

The **regexprep()** function’s syntax is given below:

**Example**

In this example, we use MATLAB’s **regexprep() **function to remove spaces from the given string str.

new_Str = regexprep(Str,'\s+','')

## 5: How to Remove Spaces in a String Using the strrep() Function?

The **strrep() **is a built-in function in MATLAB used for finding and replacing the substrings in the given string. This function can also be used for removing spaces from a string by finding a string of spaces and replacing it with a string having no space. This function accepts a string, a substring1 (used for finding occurrences), and a substring2 (used for replacing the occurrences) as arguments and returns the manipulated string.

**Syntax**

The **strrep() **function’s syntax is given below:

**Example**

The given MATLAB code uses the **strrep()** function to remove spaces from the given string str.

new_Str = strrep(Str,' ','')

## Conclusion

MATLAB is a beneficial high-performance programming tool having a huge library of built-in functions to perform different string operations. One such operation is removing spaces in a string. This guide has provided five built-in functions **strtrim(), erase(), deblank(),** **regexprep(),** and **strrep()** to perform this task using some examples.