Truncate strings to maximum length

I have an array of strings (new strings, not old character arrays) of varying lengths, and I would like to truncate them to below some maximum length. I thought that extractBefore would be helpful, but it fails for strings shorter than the truncation length.
>> str = ["ab", "cdefg"];
>> extractBefore(str, 3) % this works fine
ans =
1×2 string array
"ab" "cd"
>> extractBefore(str, 4) % this fails
Error using extractBefore
Numeric value exceeds the number of characters in element 1.
This is my current solution:
>> arrayfun(@(s)extractBefore(s, min(4, s.strlength())+1), str)
ans =
1×2 string array
"ab" "cdef"
However, this is awkward and difficult to read.
Is there no ready-made functionality for doing this?

1 commentaire

dpb
dpb le 21 Oct 2024
"Is there no ready-made functionality for doing this?"
Amazingly, no...with everything they did add, the equivalent of MID$, LEFT$, RIGHT$ are not available in a directly-callable functional form.

Connectez-vous pour commenter.

Réponses (2)

Jan
Jan le 16 Fév 2023
Modifié(e) : Jan le 16 Fév 2023
Omit arrayfun:
str = ["ab", "cdefg"];
extractBefore(str, min(4, str.strlength())+1)
ans = 1×2 string array
"ab" "cdef"
If this still looks to clumsy, write your own function:
limitWidth(str, 4)
function s = limitWidth(s, n)
s = extractBefore(s, min(n, s.strlength()) + 1);
end
Stephen23
Stephen23 le 16 Fév 2023
Modifié(e) : Stephen23 le 31 Oct 2024
str = ["", "ab", "1234", "cdefgh"];
out = regexprep(str,'^(.{1,4}).*?$','$1') % LEFT
out = 1x4 string array
"" "ab" "1234" "cdef"
out = regexprep(str,'^.*?(.{1,4})$','$1') % RIGHT
out = 1x4 string array
"" "ab" "1234" "efgh"
or (but note the behavior of the empty string):
out = regexp(str,'^.{1,4}','match','once') % LEFT
out = 1x4 string array
<missing> "ab" "1234" "cdef"
out = regexp(str,'.{1,4}$','match','once') % RIGHT
out = 1x4 string array
<missing> "ab" "1234" "efgh"

4 commentaires

dpb
dpb le 29 Oct 2024
Now how about MID$, @Stephen23? :)
Stephen23
Stephen23 le 30 Oct 2024
Modifié(e) : Stephen23 le 31 Oct 2024
str = ["", "ab", "1234", "cdefgh"];
out = regexprep(str,'^(.*?)(.{1,4})(??@sprintf(".{%u}",numel($1)))$','$2') % MID
out = 1x4 string array
"" "ab" "1234" "defg"
Note that MID is a somewhat ill-defined task: for odd vs even numbers of characters there could easily be quite different expected outputs (I can think of three reasonable interpretations). The above returns up to the maximum requested characters, ensuring that the same number of characters are trimmed from both ends.
dpb
dpb le 31 Oct 2024
Modifié(e) : dpb le 31 Oct 2024
@Stephen23, by writing the function names with the $ sign, I was presuming it would be clear the intent was to mimic BASIC (and VBA) which originally required the trailing $ on string variables. Current dialects have dropped the $ convention, of course, but the functionality is retained.
The definition of MID$
res=mid(str,start,[len])
to pick up to len characters from str beginning at start position in str. If omitted, the rest of the original string is returned, in which case it mimics RIGHT$.
It doesn't mean to try to split the string midway so there's no dependency on whether the string length is even/odd nor any ambiguity in the result.
Perhaps:
str = ["", "ab", "1234", "cdefgh"];
out = regexprep(str,'^.{0,2}(.{0,4}).*$','$1') % MID(START,LEN)
out = 1x4 string array
"" "" "34" "efgh"

Connectez-vous pour commenter.

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Produits

Version

R2022b

Tags

Question posée :

DNF
le 16 Fév 2023

Commenté :

le 31 Oct 2024

Community Treasure Hunt

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

Start Hunting!

Translated by