Truncate strings to maximum length
조회 수: 125 (최근 30일)
이전 댓글 표시
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
dpb
2024년 10월 21일
"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.
답변 (2개)
Stephen23
2023년 2월 16일
편집: Stephen23
2024년 10월 31일 0:07
str = ["", "ab", "1234", "cdefgh"];
out = regexprep(str,'^(.{1,4}).*?$','$1') % LEFT
out = regexprep(str,'^.*?(.{1,4})$','$1') % RIGHT
or (but note the behavior of the empty string):
out = regexp(str,'^.{1,4}','match','once') % LEFT
out = regexp(str,'.{1,4}$','match','once') % RIGHT
댓글 수: 4
dpb
2024년 10월 31일 11:47
편집: dpb
2024년 10월 31일 13:21
@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.
Stephen23
2024년 10월 31일 13:28
Perhaps:
str = ["", "ab", "1234", "cdefgh"];
out = regexprep(str,'^.{0,2}(.{0,4}).*$','$1') % MID(START,LEN)
참고 항목
카테고리
Help Center 및 File Exchange에서 Error Detection and Correction에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!