如何在MATLAB中删除字符串中的空格?
在本文中,我们将讨论如何在 MATLAB 中借助 isspace()、find()、strrep() 和 regexprep() 函数从字符串中删除空格。
使用 isspace()
isspace()函数用于识别 ASCII 空格元素。 isspace(' 字符串')用于查找指定' 字符串'中存在的空格。
Syntax:
isspace(‘string’)
示例 1:
Matlab
% MATLAB code for Space removal in
% string using isspace()
% Initializing a string
String = 'G F G';
% Calling the find() function
% along with isspace() function
% over the above string to remove
% the white spaces
New_String = String(find(~isspace(String)))
Matlab
% MATLAB code for replacing space with
% null using the
% isspace() function
% Initializing a string
String = 'G e e k s f o r G e e k s';
String(isspace(String)) = []
Matlab
% MATLAB code for space removal in
% string using strrep( )
% Initializing a string
String = 'G e e k s f o r G e e k s';
% Replacing space with null using the
% strrep() function over the above string
New_String = strrep(String,' ','')
Matlab
% MATLAB code for regexprep method
% for string space removal
% Initializing a string
String = 'G e e k s f o r G e e k s';
% Replacing space with null using the
% regexprep() function over the above string
New_String = regexprep(String, '\s+', '')
Matlab
% MATLAB code for space remove in string
% using deblack()
% 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 deblack() over
% above string to remove
% tab and whitespace characters
New_String = deblank(String);
% Getting the specified string
% without trailing tab and whitespace
['|' New_String '|']
Matlab
% MATLAB code for convert character array
% into string then remove space
% Specifying a character array with
% space and tab character
char = ['gfg';
'GFG ';
'GeeksforGeeks '];
% Converting the above character array into
% string
String = string(char);
% Calling the deblank() over
% above string to remove
% tab and whitespace characters
New_String = deblank(String)
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 '|']
Matlab
% MATLAB code for strrim()
% Specifying a character array with
% space and tab character
char = [' gfg';
' GFG ';
' GeeksforGeeks '];
% 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)
Matlab
% MATLAB code for space removal
% in string using erase()
% Initializing a string array
A = ["gfg - GFG"]
% Calling the erase() function
% over the above string array
B = erase(A, " ")
Matlab
% MATLAB code for space removal in string
% using equality operator
% Initializing a string
String = 'G e e k s f o r G e e k s';
% Changing the above String by setting
% locations with spaces equal to null
String(String == ' ') = []
Matlab
% MATLAB code for Space removal
% in string using inequality and non-space
% elements method
String = 'G e e k s f o r G e e k s';
% Extracting non-space elements
New_String = String(String ~= ' ')
输出:
New_String = GFG
示例 2:
MATLAB
% MATLAB code for replacing space with
% null using the
% isspace() function
% Initializing a string
String = 'G e e k s f o r G e e k s';
String(isspace(String)) = []
输出:
String = GeeksforGeeks
使用 strrep()
strrep()函数用于查找和替换子字符串。 strrep(string1, string2, string3)用于将字符串'string1' 中所有出现的字符串'string2' 替换为字符串'string3'。
Syntax:
strrep(string1, string2, string3)
例子:
MATLAB
% MATLAB code for space removal in
% string using strrep( )
% Initializing a string
String = 'G e e k s f o r G e e k s';
% Replacing space with null using the
% strrep() function over the above string
New_String = strrep(String,' ','')
输出:
New_String = GeeksforGeeks
使用 regexprep()
regexprep()函数用于使用正则表达式替换文本。
Syntax:
regexprep(str, expression, replace)
例子:
MATLAB
% MATLAB code for regexprep method
% for string space removal
% Initializing a string
String = 'G e e k s f o r G e e k s';
% Replacing space with null using the
% regexprep() function over the above string
New_String = regexprep(String, '\s+', '')
输出:
New_String = GeeksforGeeks
使用 deblank()
deblank()函数用于从指定字符串删除尾随空格或制表字符和空字符,并返回不带尾随空格的结果。
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.
示例 1:
MATLAB
% MATLAB code for space remove in string
% using deblack()
% 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 deblack() over
% above string to remove
% tab and whitespace characters
New_String = deblank(String);
% Getting the specified string
% without trailing tab and whitespace
['|' New_String '|']
输出:
ans = | gfg |
ans = | gfg|
示例 2
MATLAB
% MATLAB code for convert character array
% into string then remove space
% Specifying a character array with
% space and tab character
char = ['gfg';
'GFG ';
'GeeksforGeeks '];
% Converting the above character array into
% string
String = string(char);
% Calling the deblank() over
% above string to remove
% tab and whitespace characters
New_String = deblank(String)
输出:
New_String =
"gfg"
"GFG"
"GeeksforGeeks"
使用 strtrim()
strtrim()函数用于从指定的字符串删除前导和尾随空格字符,并将结果作为没有任何前导和尾随空格的新字符串返回。
Syntax:
strtrim(string)
参数:此函数接受如下所示的参数:
- 字符串:这是带有前导和尾随空格或制表字符的指定字符串。
返回值:它返回一个没有尾随或前导空格或制表字符的新字符串。
示例 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 '|']
输出:
ans = | gfg |
ans = |gfg|
示例 2:
MATLAB
% MATLAB code for strrim()
% Specifying a character array with
% space and tab character
char = [' gfg';
' GFG ';
' GeeksforGeeks '];
% 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)
输出:
New_String =
"gfg"
"GFG"
"GeeksforGeeks"
使用擦除()
erase( 字符串, match)函数用于删除给定字符串中所有出现的指定匹配项并返回剩余的文本。
Syntax:
erase(string, match)
参数:该函数接受两个参数,如下图所示:
- 字符串:这是要从中删除匹配项的指定字符串。
- 匹配:这是指定的匹配。
返回值:它返回一个新字符串作为没有匹配部分的剩余文本。
示例 1:
MATLAB
% MATLAB code for space removal
% in string using erase()
% Initializing a string array
A = ["gfg - GFG"]
% Calling the erase() function
% over the above string array
B = erase(A, " ")
输出:
A = gfg - GFG
B = gfg-GFG
使用关系运算符
现在,让我们看看使用关系运算符和零空间的概念来去除空间的两种不同方法。这里我们使用等式 (==) 和不等式 (~=) 关系运算符。关系运算符使用不同的运算符定量比较操作数。
示例 1:
MATLAB
% MATLAB code for space removal in string
% using equality operator
% Initializing a string
String = 'G e e k s f o r G e e k s';
% Changing the above String by setting
% locations with spaces equal to null
String(String == ' ') = []
输出:
String = GeeksforGeeks
示例 2:
MATLAB
% MATLAB code for Space removal
% in string using inequality and non-space
% elements method
String = 'G e e k s f o r G e e k s';
% Extracting non-space elements
New_String = String(String ~= ' ')
输出:
New_String = GeeksforGeeks