How to remove last characters from String using mod?

35 vues (au cours des 30 derniers jours)
Element
Element le 5 Nov 2019
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

Réponse acceptée

John D'Errico
John D'Errico le 5 Nov 2019
Modifié(e) : John D'Errico le 5 Nov 2019
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.

Plus de réponses (1)

Element
Element le 5 Nov 2019
Thank you, you have clealy indicated error in my thinking and code. Imlemented and works. Thanks

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by