How to get rid of spaces between string?

I have to work out a way to remove the spaces in my morse code output. SO far I have something like this:
...
Word=input('Please enter a word:','s');
Word=upper(Word);
Valid=1;
str = ' ';
for Character=Word
switch Character
...
case ' '
Code='/';
otherwise
Valid=0;
end

 採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 12 月 12 日
編集済み: Mohammad Abouali 2014 年 12 月 12 日

1 投票

Pam: You have two options:
Option (1) MC_1 ... MC_Z had a white space at end of them. Remove them. If I remember correctly change MC_1='.---- ' to MC_1='.----' , i.e. removing the space at the end. Then there would be no space in to begin with, so there is no need to remove them anymore
Pretty much change this part of your code:
MC_1='.---- '; MC_2='..--- '; MC_3='...-- ';
MC_4='....- '; MC_5='..... '; MC_6='-.... ';
MC_7='--... '; MC_8='---.. '; MC_9='----. ';
MC_0='----- '; MC_A='.- '; MC_B='-... ';
MC_C='-.-. '; MC_D='-.. '; MC_E='. ';
MC_F='..-. '; MC_G='--. '; MC_H='.... ';
MC_I='.. '; MC_J='.--- '; MC_K='-.- ';
MC_L='.-.. '; MC_M='-- '; MC_N='-. ';
MC_O='--- '; MC_P='.--. '; MC_Q='--.- ';
MC_R='.-. '; MC_S='... '; MC_T='- ';
MC_U='..- '; MC_V='...- '; MC_W='.-- ';
MC_X='-..- '; MC_Y='-.-- '; MC_Z='--.. ';
to
MC_1='.----'; MC_2='..---'; MC_3='...--';
MC_4='....-'; MC_5='.....'; MC_6='-....';
MC_7='--...'; MC_8='---..'; MC_9='----.';
MC_0='-----'; MC_A='.-'; MC_B='-...';
MC_C='-.-.'; MC_D='-..'; MC_E='.';
MC_F='..-.'; MC_G='--.'; MC_H='....';
MC_I='..'; MC_J='.---'; MC_K='-.-';
MC_L='.-..'; MC_M='--'; MC_N='-.';
MC_O='---'; MC_P='.--.'; MC_Q='--.-';
MC_R='.-.'; MC_S='...'; MC_T='-';
MC_U='..-'; MC_V='...-'; MC_W='.--';
MC_X='-..-'; MC_Y='-.--'; MC_Z='--..';
If you are still using the code that you asked before.
Option (2) If
Word='-. --- / .... ..'
So it has spaces, the easiest way to remove the spaces is this:
Word=Word(Word~=' ');

10 件のコメント

Mohammad Abouali
Mohammad Abouali 2014 年 12 月 12 日
Ok, Then I think the best and easiest is to go with option (1). remove any spaces in the definition of MC_1 but keep ' / ' as it is with one space before and another after it.
Pam
Pam 2014 年 12 月 12 日
alright, but will it not show up more than twice when I have more than one space?
Pam
Pam 2014 年 12 月 12 日
編集済み: Pam 2014 年 12 月 14 日
I did this:
Word=input('Please enter a word:','s');
Word=upper(Word);
Valid=1;
Word = strtrim(Word);
for Character=Word
switch Character
....
Mohammad Abouali
Mohammad Abouali 2014 年 12 月 12 日
編集済み: Mohammad Abouali 2014 年 12 月 12 日
If you have two spaces next to each other you would see ' / / '
you could remove them using something like this:
S='Hello Hello'; % an string with double space between Hello
subS=' ';
for i=2:numel(S)
S=strrep(S,subS,' ');
subS=[subS ' '];
end
After this loop S will not have any repeated spaces. So run that on the input string before passing it to your morse code convertor.
Pam
Pam 2014 年 12 月 13 日
編集済み: Pam 2014 年 12 月 14 日
MC_X='-..-';MC_Y='-.--'; MC_Z='--..';
Word=input('Please enter a word:','s');
Word=upper(Word);
Valid=1;
Word2=' ';
subWord2=' ';
for i=2:numel(Word2)
Word=strrep(Word,subWord2,' ');
subWord=[subS ' '];
end
for Character=Word
switch Character
case '1'
I did something like this using that but I still got two // when I input two words with two spaces in between I think I may have used it incorrectly
Mohammad Abouali
Mohammad Abouali 2014 年 12 月 13 日
So change this part of your code:
Word2=' ';
subWord2=' ';
for i=2:numel(Word2)
Word=strrep(Word,subWord2,' ');
subWord=[subS ' '];
end
to
Word=strjoin(strsplit(Word));
Pam
Pam 2014 年 12 月 13 日
oh ok thank you this really helps. I was wondering if this could be done in some kind of loop format?
Mohammad Abouali
Mohammad Abouali 2014 年 12 月 13 日
for index=1:length(Word)
Word(Word==' ') = '';
end
This part of you code check for spaces and removes them. You do not want to remove spaces. You want to remove double spaces.
Pam
Pam 2014 年 12 月 14 日
編集済み: Pam 2014 年 12 月 14 日
Oh I didnt realize I was doing this.I tried out the t=strjoin(strsplit(s)) method and it works but I was trying to do it in a loop form so I would understand it better. Is there anyway to do this in a loop?
Mohammad Abouali
Mohammad Abouali 2014 年 12 月 14 日
subS=' ';
for i=2:numel(S)
S=strrep(S,subS,' ');
subS=[subS ' '];
end
loop form

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2014 年 12 月 12 日

1 投票

The easiest to get rid of extra spaces:
s = 'a string with some weird spaces ';
t = strjoin(strsplit(s))

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

Pam
2014 年 12 月 12 日

編集済み:

Pam
2014 年 12 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by