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


MATLAB 提供多種方法和函式來去除字串中的空格。在本教程中,我們將介紹常用的去除字串中多餘空格的方法。讓我們開始吧。

使用“isspace”函式去除字串中的空白字元

在 MATLAB 中,“isspace”函式是一個內建函式,用於確定和去除字串中存在的 ASCII 空白字元。

語法

string_without_space = isspace('input_string');

示例

讓我們來看一個 MATLAB 程式設計示例,使用此函式去除字串中的空格。

% 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”函式去除字串中空格的示例。

% 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”函式顯示帶有和不帶空格的字串。

使用“regexprep”函式去除字串中的空白字元

在 MATLAB 中,“regexprep”是一個內建函式,可用於去除字串中出現的空格。

語法

string_wo_spaces = regexprep(str, expression, replace);

這裡,“str”是帶有空格的字串,“expression”是匹配字串中空格的正則表示式,“replace”是空字串或無空格字元。

示例

讓我們來看一個示例,瞭解如何在 MATLAB 中使用此函式去除空格。

% 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”是包含尾隨空格或製表符空格的輸入字串。

示例

讓我們來看一個示例,瞭解如何使用此函式去除字串中的空格。

% 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”函式去除字串中的前導和尾隨空格。

% 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

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.