Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何删除字符串之间的额外空格,matlab?_String_Matlab_For Loop - Fatal编程技术网

String 如何删除字符串之间的额外空格,matlab?

String 如何删除字符串之间的额外空格,matlab?,string,matlab,for-loop,String,Matlab,For Loop,我已经创建了一个脚本来将文本转换成莫尔斯电码,现在我想修改它,使其在单词之间包含一个斜杠,比如空格,斜杠,莫尔斯电码单词之间的空格。我知道我的循环在主循环之前是不正确的,我想按照之前的说明进行修复,我真的需要帮助谢谢!!!: ... Word=input('Please enter a word:','s'); ... Code=MC_1; ... case ' ' Code='/' otherwise

我已经创建了一个脚本来将文本转换成莫尔斯电码,现在我想修改它,使其在单词之间包含一个斜杠,比如空格,斜杠,莫尔斯电码单词之间的空格。我知道我的循环在主循环之前是不正确的,我想按照之前的说明进行修复,我真的需要帮助谢谢!!!:

...

Word=input('Please enter a word:','s');
  ...
             Code=MC_1;
    ...

    case ' '
         Code='/'
    otherwise 
         Valid=0;
end
if Valid
         fprintf('%s ',Code);
else
         disp('Input has invalid characters!')
         break
end

我知道您希望编写一个循环来删除单词之间的多个空格,但是在您的特定问题中删除空白的最佳方法是使用,特别是使用。正则表达式用于搜索较大字符串中的特定模式/子字符串。在本例中,我们试图找到的是由多个空格组成的子字符串
regexprep
查找与模式匹配的子字符串,并用另一个字符串替换它们。在我们的例子中,您将搜索字符串中包含至少一个以上空白字符的任何子字符串,并将其替换为一个空白字符。此外,我还看到您使用修剪了字符串的前导空格和尾随空格,这很好。现在,您只需调用
regexprep
,如下所示:

Word = regexprep(Word, '\s+', ' ');
\s+
是用于查找至少一个空格字符的正则表达式。然后我们用一个空格来替换它。因此,假设我们将此字符串存储在
Word
中:

Word = '   hello    how    are   you   ';
对前导空格和尾随空格进行修剪,然后按照我们前面讨论的方式调用
regexprep
,这样可以得到:

Word = strtrim(Word);
Word = regexprep(Word, '\s+', ' ')

Word =

hello how are you
Word =

hello how are you
如您所见,前导和尾随空格已使用strtrim
strtrim
删除,正则表达式负责处理中间的其余空格


但是,如果您死心塌地地想使用循环,那么您可以使用
逻辑
变量,当我们检测到空白时,该变量被设置为
,然后我们使用该变量并跳过其他空白字符,直到我们碰到一个不是空白的字符。然后我们将放置空间,然后
/
,然后放置空间,然后继续。换言之,请执行以下操作:

Word = strtrim(Word); %// Remove leading and trailing whitespace
space_hit = false; %// Initialize space encountered flag
Word_noSpace = []; %// Will store our new string
for index=1:length(Word) %// For each character in our word
    if Word(index) == ' ' %// If we hit a space
       if space_hit %// Check to see if we have already hit a space
          continue; %// Continue if we have
       else
          Word_noSpace = [Word_noSpace ' ']; %// If not, add a space, then set the flag
          space_hit = true;
       end
    else
       space_hit = false; %// When we finally hit a non-space, set back to false
       Word_noSpace = [Word_noSpace Word(index)]; %// Keep appending characters
    end
end
Word = Word_noSpace; %// Replace to make compatible with the rest of your code

for Character = Word %// Your code begins here
   ...
   ...
上面的代码所做的是,我们有一个名为
Word\u noSpace
的空字符串,其中包含我们的单词,没有额外的空格,这些空格将替换为一个空格。循环遍历每个字符,如果遇到空格,我们会检查是否已经遇到空格。如果有,就继续循环。如果没有,则连接一个空格。一旦我们最终命中了一个非空格字符,我们只需将这些非空格字符添加到这个新字符串中。结果将是一个没有额外空格的字符串,这些字符串将替换为一个空格

在修剪前导和尾随空格后运行上述代码,这样可以得到:

Word = strtrim(Word);
Word = regexprep(Word, '\s+', ' ')

Word =

hello how are you
Word =

hello how are you
似乎以你想要的方式格式化莫尔斯电码。