简体   繁体   English

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

[英]How to remove extra spaces in between string, matlab?

I have created a script to convert text to morsecode, and now I want to modify it to include a slash between words.So something like space slash space between morsecode words. 我已经创建了一个脚本来将文本转换为morsecode,现在我想修改它以包含单词之间的斜杠。这就像morsecode单词之间的空格斜杠空间。 I know my loop before the main loop is incorrect and I want to fix it to do as stated before I just really need help Thank You!!!: 我知道我的循环在主循环之前是不正确的,我想修复它,如前所述,我真的需要帮助谢谢!!!

...

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

I know you want to write a loop to remove multiple spaces in between words, but the best way to remove white space in your particular problem would be to use regular expressions , specifically with regexprep . 我知道你想编写一个循环来删除单词之间的多个空格,但在你的特定问题中删除空格的最好方法是使用正则表达式 ,特别是使用regexprep Regular expressions are used to search for particular patterns / substrings within a larger string. 正则表达式用于搜索较大字符串中的特定模式/子字符串。 In this case, what we are trying to find are substrings that consist of more than one whitespace. 在这种情况下,我们试图找到的是由多个空格组成的子串。 regexprep finds substrings that match a pattern, and replaces them with another string. regexprep查找与模式匹配的子字符串,并将其替换为另一个字符串。 In our case, you would search for any substrings within your string that contain at least one more whitespace characters, and replace them with a single whitespace character. 在我们的示例中,您将搜索字符串中包含至少一个空格字符的任何子字符串,并用单个空格字符替换它们。 Also, I see that you've trimmed both leading and trailing whitespace for the string using strtrim , which is great. 另外,我看到你使用strtrim修剪了字符串的前导和尾随空格,这很棒。 Now, all you need to do is call regexprep like so: 现在,您需要做的就是调用regexprep如下所示:

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

\\s+ is the regular expression for finding at least one white space character . \\s+是用于查找至少一个空格字符的正则表达式。 We then replace this with a single whitespace. 然后我们用一个空格替换它。 As such, supposing we had this string stored in Word : 因此,假设我们将此字符串存储在Word

Word = '   hello    how    are   you   ';

Doing a trim of leading and trailing whitespace, then calling regexprep in the way we talked about thus gives: 修剪前导空格和尾随空格,然后按照我们讨论的方式调用regexprep ,从而得到:

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

Word =

hello how are you

As you can see, the leading and trailing white space was removed with strtrim , and the regular expression takes care of the rest of the spaces in between. 如您所见,使用strtrim删除了前导和尾随空格,正则表达式负责处理strtrim的其余空格。


However, if you are dead set on using a loop, what you can do is use a logical variable which is set to true when we detect a white space, and then we use this variable and skip other white space characters until we hit a character that isn't a space. 但是,如果您使用循环设置为死,那么您可以使用的logical变量在我们检测到空格时设置为true ,然后我们使用此变量并跳过其他空白字符, 直到我们点击一​​个字符那不是空间。 We would then place our space, then / , then space, then continue. 然后我们将我们的空间,然后/ ,然后空间,然后继续。 In other words, do something like this: 换句话说,做这样的事情:

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
   ...
   ...

What the above code does is that we have an empty string called Word_noSpace that will contain our word with no extra spaces, and those spaces replaced with a single whitespace. 上面的代码所做的是我们有一个名为Word_noSpace的空字符串,它将包含我们的单词,没有多余的空格,并且这些空格被一个空格替换。 The loop goes through each character, and should we encounter a space, we check to see if we have already encountered a space. 循环遍历每个字符,如果我们遇到空格,我们检查是否已经遇到空格。 If we have, just continue on in the loop. 如果我们有,只需继续循环。 If we haven't, then concatenate a whitespace. 如果我们没有,那么连接一个空格。 Once we finally hit a non-space character, we simply just add those characters that are not spaces to this new string. 一旦我们最终命中了非空格字符,我们只需将那些不是空格的字符添加到这个新字符串中。 The result will be a string with no extra spaces, and those are replaced with a single white space. 结果将是一个没有多余空格的字符串,并用一个空格替换。

Running the above code after you trim the leading and trailing white space thus gives: 修剪前导和尾随空格后运行上面的代码,从而得出:

Word =

hello how are you

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM