How to use strtrim() In MATLAB

 

The strtrim() function is used to remove leading and trailing whitespace characters from the specified string and returns the result as a new string without any leading and trailing whitespaces.

 

Syntax:

strtrim(string)

Parameters: This function accepts a parameter which is illustrated below:
 

  • string: This is the specified string with leading and trailing whitespace or tab characters.

Return values: It returns a new string without trailing or leading whitespace or tab characters.

 

Example 1:

Matlab




% Specifying a string 'gfg'
% along with a tab and
% whitespace character
String = sprintf('\t gfg \t');
 
% Adding '|' character to the
% above string
['|' String '|']
 
% Calling the strtrim() function over
% above string to remove leading and
% trailing tab and whitespace characters
New_String = strtrim(String);
 
% Getting the specified string
% without leading and trailing tab and
% whitespace
['|' New_String '|']


 
 

Output:

ans = |  gfg  |
ans = |gfg|

 

Example 2:

Matlab




% MATLAB code for strrim()
% Specifying a character array with
% space and tab character
char = [' gfg';
        ' GFG ';
        '   w3wiki    '];
 
% Converting the above character array
% into string
String = string(char);
 
% Calling the strtrim() over
% above string to remove leading and
% trailing tab and whitespace characters
New_String = strtrim(String)


 
 

Output:

New_String =
"gfg"    
"GFG"    
"w3wiki"  

How to remove space in a string in MATLAB?

In this article, we are going to discuss how to remove space from a string in MATLAB with the help of isspace(), find(), strrep(), and regexprep() functions.

Similar Reads

Using isspace()

The isspace() function is used to identify elements that are ASCII white spaces. isspace(‘string’) is used to find the white spaces present in the specified ‘string’....

Using strrep()

...

Using regexprep()

...

Using deblank()

...

Using strtrim()

...

Using erase()

The regexprep() function is used to replace text using regular expressions....

Using relational operator

...

Contact Us