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.
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’.
Syntax:
isspace(‘string’)
Example 1:
Matlab
String = 'G F G' ;
New_String = String(find(~isspace(String)))
|
Output:
New_String = GFG
Example 2:
Matlab
String = 'G e e k s f o r G e e k s' ;
String(isspace(String)) = []
|
Output:
String = GeeksforGeeks
Using strrep()
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
String = 'G e e k s f o r G e e k s' ;
New_String = strrep(String, ' ' , '' )
|
Output:
New_String = GeeksforGeeks
Using regexprep()
The regexprep() function is used to replace text using regular expressions.
Syntax:
regexprep(str, expression, replace)
Example:
Matlab
String = 'G e e k s f o r G e e k s' ;
New_String = regexprep(String, '\s+' , '' )
|
Output:
New_String = GeeksforGeeks
Using deblank()
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
String = sprintf( '\t gfg \t' );
[ '|' String '|' ]
New_String = deblank(String);
[ '|' New_String '|' ]
|
Output:
ans = | gfg |
ans = | gfg|
Example 2
Matlab
char = [ 'gfg' ;
'GFG ' ;
'GeeksforGeeks ' ];
String = string(char);
New_String = deblank(String)
|
Output:
New_String =
"gfg"
"GFG"
"GeeksforGeeks"
Using strtrim()
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
String = sprintf( '\t gfg \t' );
[ '|' String '|' ]
New_String = strtrim(String);
[ '|' New_String '|' ]
|
Output:
ans = | gfg |
ans = |gfg|
Example 2:
Matlab
char = [ ' gfg' ;
' GFG ' ;
' GeeksforGeeks ' ];
String = string(char);
New_String = strtrim(String)
|
Output:
New_String =
"gfg"
"GFG"
"GeeksforGeeks"
Using erase()
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
A = [ "gfg - GFG" ]
B = erase(A, " " )
|
Output:
A = gfg - GFG
B = gfg-GFG
Using relational operator
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
String = 'G e e k s f o r G e e k s' ;
String(String == ' ' ) = []
|
Output:
String = GeeksforGeeks
Example 2:
Matlab
String = 'G e e k s f o r G e e k s' ;
New_String = String(String ~= ' ' )
|
Output:
New_String = GeeksforGeeks
Last Updated :
05 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...