如何在MATLAB中去除字符串中的空格?


MATLAB 提供多种方法和函数来去除字符串中的空格。在本教程中,我们将介绍常用的去除字符串中多余空格的方法。让我们开始吧。

使用“isspace”函数去除字符串中的空白字符

在 MATLAB 中,“isspace”函数是一个内置函数,用于确定和去除字符串中存在的 ASCII 空白字符。

语法

string_without_space = isspace('input_string');

示例

让我们来看一个 MATLAB 编程示例,使用此函数去除字符串中的空格。

Open Compiler
% MATLAB code to remove white space from a string % Specify a sample string with white space S = 'Tutorials Point India Limited'; % Remove the white space using "isspace" function string_without_spcaes = S(~isspace(S)); % Display the string with and without spaces disp('The string with space: '); disp(S); disp('The string without space: '); disp(string_without_spcaes);

输出

运行此代码后,将产生以下 **输出** −

The string with space: 
Tutorials Point India Limited
The string without space: 
TutorialsPointIndiaLimited

代码解释

在这个例子中,我们首先定义一个包含空格的字符串。然后,我们使用逻辑索引和“isspace”函数去除字符串中的空格。最后,我们使用“disp”函数显示带有和不带空格的字符串。

使用“strrep”函数去除字符串中的空白字符

在 MATLAB 中,“strrep”是另一个内置函数,可用于从字符串中去除空格。此函数使用查找和替换方法来去除字符串中出现的空格。

string_wo_spaces = strrep(S1, S2, S3);

这里,S1、S2 和 S3 是字符串。此函数将字符串“S1”中所有出现的字符串“S2”替换为字符串“S3”。

示例

让我们来看一个使用 MATLAB 中的“strrep”函数去除字符串中空格的示例。

Open Compiler
% MATLAB code to remove white space from a string % Specify a sample string with white spaces S = 'Tutorials Point India Limited'; % Remove the white space using "strrep" function string_without_spcaes = strrep(S, ' ', ''); % Display the string with and without spaces disp('The string with space: '); disp(S); disp('The string without space: '); disp(string_without_spcaes);

输出

运行此代码后,将产生以下 **输出** −

The string with space: 
Tutorials Point India Limited
The string without space: 
TutorialsPointIndiaLimited

代码解释

在这个例子中,我们首先创建一个包含空格的字符串,然后调用“strrep”函数将所有空格替换为空格。最后,我们使用“disp”函数显示带有和不带空格的字符串。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

使用“regexprep”函数去除字符串中的空白字符

在 MATLAB 中,“regexprep”是一个内置函数,可用于去除字符串中出现的空格。

语法

string_wo_spaces = regexprep(str, expression, replace);

这里,“str”是带有空格的字符串,“expression”是匹配字符串中空格的正则表达式,“replace”是空字符串或无空格字符。

示例

让我们来看一个示例,了解如何在 MATLAB 中使用此函数去除空格。

Open Compiler
% MATLAB code to remove spaces in a string % Define a sample string with spaces S = 'Tutorials Points India Limited'; % Specify the regular expression to match spaces E = '\s+'; % Remove the spaces from the string string_wo_spaces = regexprep(S, E, ''); % Display the string with and without spaces disp('String with spaces: '); disp(S); disp('String without spaces: '); disp(string_wo_spaces);

输出

运行此代码后,将产生以下 **输出** −

String with spaces: 
Tutorials Points India Limited
String without spaces: 
TutorialsPointsIndiaLimited

代码解释

在这个例子中,我们首先创建一个包含空格的字符串。然后,我们定义一个正则表达式模式“E”来匹配字符串中出现的空格。

之后,我们调用“regexprep”函数将输入字符串中出现的空格字符替换为空格字符。最后,我们使用“disp”函数显示带有和不带空格的字符串。

使用“deblank”函数去除字符串中的空白字符

“deblank”是 MATLAB 中另一个用于去除字符串中空格的内置函数。它可以用于去除字符串中尾随空格或制表符空格。

语法

StringWithoutSpace = deblank(str);

这里,“str”是包含尾随空格或制表符空格的输入字符串。

示例

让我们来看一个示例,了解如何使用此函数去除字符串中的空格。

Open Compiler
% MATLAB code remove trailing tab spaces in a string % Specify a string with training tab spaces S = sprintf('\t Tutorials-Point \t'); % Display the string with '$' symbol for clarity disp(['Input string: $' S '$']); % Remove the trailing tab spaces from the string string_wo_space = deblank(S); % Display the string without trailing tab spaces disp(['String without space: $' string_wo_space '$']);

输出

运行此代码后,将产生以下 **输出** −

Input string:			$	 Tutorials-Point 	$
String without space: 	$	 Tutorials-Point$

代码解释

在这个例子中,可以看到“deblank”函数已经去除了字符串中的尾随空格。

使用“strtrim”函数去除字符串中的空白字符

在 MATLAB 中,还有一个名为“strtrim”的内置函数,可用于去除字符串中前导和尾随空格。

语法

string_wo_spaces = strtrim(input-string);

示例

让我们来看一个示例,了解如何使用 MATLAB 中的“strtrim”函数去除字符串中的前导和尾随空格。

Open Compiler
% MATLAB code to remove leading and trailing spaces in a string % Create an input string with leading and trailing spaces S = sprintf('\t Tutorials-Point \t'); % Display the input string with '$' symbol for clarity disp(['Input String: $' S '$']); % Remove leading and trailing spaces string_wo_spaces = strtrim(S); % Display the output string without leading and trailing spaces disp(['Output String: $' string_wo_spaces '$']);

输出

运行此代码后,将产生以下 **输出** −

Input String:	$	 Tutorials-Point 	$
Output String:	$Tutorials-Point$

解释

在这个例子中,可以看到“strtrim”函数已经去除了字符串中的前导和尾随空格。

结论

这就是关于使用 MATLAB 去除字符串中空格的全部内容。在 MATLAB 中,有各种内置函数可用于去除字符串中存在的空格。在本教程中,我解释了所有常用的去除字符串中空格的函数。

更新于:2023年10月9日

浏览量:359

启动你的 职业生涯

通过完成课程获得认证

开始学习
广告