この質問は閉じられています。 編集または回答するには再度開いてください。

Looking for string sequence within excel table with an unidentified number of spaces inbetween characters

1 ビュー (過去 30 日間)
Saeid 2018 年 1 月 5 日
終了済み: MATLAB Answer Bot 2021 年 8 月 20 日
Let's assume that I am looking for the sentence:
"This sentence has 27 characters"
inside a large excel table, which is composed on both string and numbers. However, this sentence could be written in different number os spaces inbetween the words, e.g.
"Thissentence has27 characters"
"Thi s sente nceha s27chara cte rs"
Is there a way to write just one command that covers all possible numbers of spaces inbetween characters, as long as the sequence always stays the same?

回答 (1 件)

Chris Perkins
Chris Perkins 2018 年 1 月 8 日
編集済み: Chris Perkins 2018 年 1 月 8 日
Hi Saeid,
You can remove all spaces using "regexprep" before doing the comparison. For example,
% Setting up sample strings
testStr = 'This sentence has 27 characters';
excelStr1 = 'Th is sen tence h as 27 charac ters';
excelStr2 = 'This sent en ceha s2 7ch aracters';
% Remove spaces
testStrNoSpace = regexprep(testStr,'\s+','')
excelStr1NoSpace = regexprep(excelStr1,'\s+','')
excelStr2 = regexprep(excelStr2,'\s+','')
Here, "regexprep" is replacing each instance of one or more space characters with an empty string, effectively removing the spaces.
After stripping the space characters, you can compare your strings normally to see if they match or not.
For more information regarding "regexprep", see the following documentation link:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by