How to remove space in a string in MATLAB?
Last Updated :
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.
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
Similar Reads
How to reverse a string in MATLAB?
In this article, we are going to discuss the "Reversing of a string" in MATLAB which can be done in multiple approaches that are illustrated below: Method 1: Using Built-in Functions By using MATLAB built-in functions like reverse(), flip() and fliplr(). Built-in functions can be defined as - " The
3 min read
How to remove decimal in MATLAB?
In this article, we are going to discuss the "Removal of decimal point" in MATLAB which can be done using sprintf(), fix(), floor(), round() and num2str() functions which are illustrated below. Using sprintf() The sprintf() function is used to write formatted data to a string. Syntax: sprintf(format
3 min read
How to reverse a number in MATLAB?
In this article, we will discuss the "Reversing of a number" in MATLAB that can be done using the multiple methods which are illustrated below. Using str2num() The str2num() function is used to convert the specified character array or string to a numeric array. Syntax: Â str2num(chr) Parameters: This
3 min read
Turn a Matrix into a Row Vector in MATLAB
Conversion of a Matrix into a Row Vector. This conversion can be done using reshape() function along with the Transpose operation. This reshape() function is used to reshape the specified matrix using the given size vector. Syntax:reshape(A, sz) Parameters: This function accepts two parameters, whic
1 min read
Comparing Two Cell Arrays of Strings of Different Sizes in MATLAB
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. Cell Array: A cell Array in MATLAB is a data type that can store any type of data. Data is stored in cells in a cell array. It is init
2 min read
How to Remove Nan Values from a Matrix in MATLAB?
Removal of Nan Values from a Matrix.There are multiple methods by which we can remove Nan values from a specified matrix:. Method 1: By using rmmissing( )Â This function is used to remove missing entries or Nan values from a specified matrix. Syntaxrmmissing(A) Parameters: This function accepts a pa
2 min read
MATLAB Find Exact String in Cell Array
Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions,
2 min read
MATLAB - Read Words in a File in Reverse Order
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. It allows matrix manipulations, plotting of functions, implementation of algorithms, and creation of user interfaces Suppose we are given a text file containing the following words "I STUDY F
2 min read
Characters and Strings in MATLAB
In this article, we shall see how to deal with characters and Strings in MATLAB. A data type is an attribute/keyword that specifies the type of data that the object can hold: numeric data or text data. By default, MATLAB stores all numeric variables as double-precision floating-point values. Additio
4 min read
Find Index of Cells Containing My String in MATLAB
Cell Arrays in MATLAB are a type of array that store data in the form of cells. The benefit of using these cell arrays is that they can store data of different types as cells within a cell array only. In this article, we will see how to find a given string in a cell array. Firstly, we shall check wh
2 min read