MATLAB Answers

How to remove last characters from String using mod?

67 ビュー (過去 30 日間)
Element
Element 2019 年 11 月 5 日
Answered: Element 2019 年 11 月 5 日
Is there a way to remove last characters from String, if number of characters are for example divisible by 4?
Something like this:
rem = mod(text,4);
text = text(1:end- rem);
Code above does not work.
For example if length of text would be 23 chars, I would like to remove last 3 chars and create new String.
Thanks

  0 件のコメント

サインイン to comment.

採用された回答

John D'Errico
John D'Errico 2019 年 11 月 5 日
編集済み: John D'Errico 2019 年 11 月 5 日
It is generally a bad idea to use names for variables that are already useful function names. So here rem is one such bad idea. The rem function is similar to mod, in fact, though it behaves differently in some subtle respects. Regardless, you will do well to avoid such things.
But why did your code fail? In fact, you have the right idea. Where you failed was in the first step. You did this:
rem = mod(text,4);
text = text(1:end- rem);
The problem is you did not know what to apply mod to. You needed to take the modulus of the LENGTH of text. Text is a string vector. If you take the mod of the string itself, then expect problems.
In fact, I think you meant a character vector, not a string. Be careful here, as there is now a new class in MATLAB, called a string. It is created using double quotes. A character vector is created using single quotes. But the way you are using the text vector suggests that you have a character vector, so I will assume that to be the case. For example...
text = 'The quick brown fox jumps over the lazy dog'
text =
'The quick brown fox jumps over the lazy dog'
So text is a vector, of length 43. You want to reduce it to a length that is a multiple of 4. You cannot take the mod of text itself, but of the length of text, or the number of elements in text. The numel function tells how many total elements in the vector or array. For vectors, length applies too.
numel(text)
ans =
43
length(text)
ans =
43
So what you might do is
numtext = length(text);
numdiscarded = mod(numtext,4);
text = text(1:end - numdiscarded);
text
text =
'The quick brown fox jumps over the lazy '
As you can see, we dropped the last three characters in the vector, here, the word dog. Leaving a less complete sentence, but that is a bit irrelevant. ;-) You could have done it in one line of course, simply by compressing it all into one line, but that tends to create less readable code. I strongly recommend the use of uncompressed code. Use extra variables, IF they will make your code more clear and easy to read. That also makes your code easier to debug later on. So, while this:
text = text(1:end - mod(numel(text),4));
does work, I'd suggest the first form is a better programming style.

  0 件のコメント

サインイン to comment.

More Answers (1)

Element
Element 2019 年 11 月 5 日
Thank you, you have clealy indicated error in my thinking and code. Imlemented and works. Thanks

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by