Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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
like image 664
Sam Avatar asked Dec 14 '14 03:12

Sam


People also ask

How do you trim a string in Matlab?

Description. newStr = strip( str ) removes all consecutive whitespace characters from the beginning and end of str , and returns the result as newStr . newStr = strip( str , side ) removes all consecutive whitespace characters from the side specified by side .

How do you find spaces in a string in Matlab?

To find space characters within elements of a nonscalar string array, use the isstrprop function.

How do you extract part of a string in Matlab?

newStr = extractAfter( str , pat ) extracts the substring that begins after the substring specified by pat and ends with the last character of str . If pat occurs multiple times in str , then newStr is str from the first occurrence of pat to the end.


1 Answers

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. 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. 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. Now, all you need to do is callregexprep like so:

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

\s+ is the regular expression for finding at least one white space character. We then replace this with a single whitespace. As such, supposing we had this string stored in 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:

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.


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. 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. 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
like image 169
rayryeng Avatar answered Sep 20 '22 17:09

rayryeng