How to remove space in a string in MATLAB?

05 Aug, 2021
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.
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’.
Syntax:
isspace(‘string’)
Example 1:
Matlab
% MATLAB code for Space removal in% string using isspace()% Initializing a stringString = 'G F G'; % Calling the find() function% along with isspace() function% over the above string to remove% the white spacesNew_String = String(find(~isspace(String)))
  
Output:
New_String = GFG
Example 2: 
Matlab
% MATLAB code for replacing space with% null using the% isspace() function % Initializing a stringString = 'G e e k s f o r G e e k s';String(isspace(String)) = []
  
Output:
String = GeeksforGeeks
 
The strrep() function is used to find and replace substrings. strrep(string1, string2, string3) is used to replace all occurrences of the string ‘string2’ within string ‘string1’ with the string ‘string3’.
 
Syntax: 
strrep(string1, string2, string3)
Example:
Matlab
% MATLAB code for space removal in% string using strrep( )% Initializing a stringString = 'G e e k s f o r G e e k s'; % Replacing space with null using the% strrep() function over the above stringNew_String = strrep(String,' ','')
Output:
New_String = GeeksforGeeks
The regexprep() function is used to replace text using regular expressions.
Syntax: 
regexprep(str, expression, replace)
Example:
Matlab
% MATLAB code for regexprep method% for string space removal% Initializing a stringString = 'G e e k s f o r G e e k s'; % Replacing space with null using the% regexprep() function over the above stringNew_String = regexprep(String, '\s+', '')
  
Output:
 
New_String = GeeksforGeeks
The deblank() function is used to remove the trailing whitespace or tab characters and null characters from the specified string and returns the result without the trailing whitespace.
 
Syntax:
deblank(string)
Parameters: This function accepts a parameter which is illustrated below:
string: This is the specified string with whitespace or tab characters.
Return values: It returns a new string without trailing whitespace or tab characters.
 
Example 1:
 
Matlab
% MATLAB code for space remove in string% using deblack()% Specifying a string 'gfg'% along with a tab and% whitespace characterString = sprintf('\t gfg \t'); % Adding '|' character to the% above string['|' String '|'] % Calling the deblack() over% above string to remove% tab and whitespace charactersNew_String = deblank(String); % Getting the specified string% without trailing tab and whitespace['|' New_String '|']
  
Output:
ans = |  gfg  |
ans = |  gfg|
 
Example 2
Matlab
% MATLAB code for convert character array% into string then remove space% Specifying a character array with% space and tab characterchar = ['gfg';         'GFG ';         'GeeksforGeeks    '];          % Converting the above character array into% stringString = string(char); % Calling the deblank() over% above string to remove% tab and whitespace charactersNew_String = deblank(String)
  
Output: 
New_String =
"gfg"    
"GFG"    
"GeeksforGeeks"  
 
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 characterString = 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 charactersNew_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 characterchar = [' gfg';        ' GFG ';        '   GeeksforGeeks    ']; % Converting the above character array% into stringString = string(char); % Calling the strtrim() over% above string to remove leading and% trailing tab and whitespace charactersNew_String = strtrim(String)
  
Output:
New_String =
"gfg"    
"GFG"    
"GeeksforGeeks"  
The erase(string, match) function is used to remove all occurrences of the specified match in the given string and returns the remaining text.
 
Syntax:
erase(string, match)
 
Parameters: This function accepts two parameters, which are illustrated below:
 
string: This is the specified string from which the match is going to be removed.
match: This is the specified match.
 
Return values: It returns a new string as the remaining text without the matching part.
 
Example 1:
 
Matlab
% MATLAB code for space removal% in string using  erase()% Initializing a string arrayA = ["gfg - GFG"] % Calling the erase() function% over the above string arrayB = erase(A, " ")
  
Output:
A = gfg - GFG
B = gfg-GFG
Now, let’s see two different methods for space removal by using relational operators and the concept of the null space. Here we use equality (== ) and inequality (~=) relational operators. Relational operators compare operands quantitatively, using different operators.
 
Example 1:
Matlab
% MATLAB code for space removal in string% using equality operator% Initializing a stringString = 'G e e k s f o r G e e k s'; % Changing the above String by setting% locations with spaces equal to nullString(String == ' ') = []
  
Output:
String = GeeksforGeeks
 
Example 2:
Matlab
% MATLAB code for Space removal% in string using inequality and non-space% elements methodString = 'G e e k s f o r G e e k s'; % Extracting non-space elementsNew_String = String(String ~= ' ')
Output:
New_String = GeeksforGeeks
 
arorakashish0911
MATLAB String-Programs
MATLAB-programs
MATLAB
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Convert Three Channels of Colored Image into Grayscale Image in MATLAB?
How to Solve Histogram Equalization Numerical Problem in MATLAB?
Adaptive Histogram Equalization in Image Processing Using MATLAB
MRI Image Segmentation in MATLAB
How to detect duplicate values and its indices within an array in MATLAB?
Double Integral in MATLAB
Classes and Object in MATLAB
Forward and Inverse Fourier Transform of an Image in MATLAB
How to Normalize a Histogram in MATLAB?
How to Convert RGB Image to Binary Image Using MATLAB?