Replace Before A Specific Character
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a string of calendar duraton values and am trying to replace the year value (Because I did some separate calculations on it). I want to replace the value given by the calendar duration function with my calculated value. The value I want to replace is the number up until "y" in this string: diffStr=4026y 4 mo 13d 9h 26m 20.145s"
Is there a replaceBefore function that I can use to replace everything before the "y"?
Thank you for your help.
1 Kommentar
Stephen23
am 28 Feb. 2024
Bearbeitet: Stephen23
am 28 Feb. 2024
"I have a string of calendar duraton values"
Calendar duration objects are not string objects. It is unclear what data you really have: descriptions of data are often very unreliable.... whereas having the actual data is always accurate.
Please upload some sample data in a MAT file, by clicking the paperclip button.
Antworten (4)
Cris LaPierre
am 28 Feb. 2024
Bearbeitet: Cris LaPierre
am 28 Feb. 2024
Is it a duration or a string?
For a duration
Just to give a different solution than the others, here is one way to update a duration without changing the datatype. datevec works for matrix inputs, too.
d = calendarDuration(4026,5,13,9,26,20.145)
[Y,M,D,H,Mn,S] = datevec(d);
newYr = 2021;
D = calendarDuration(newYr,M,D,H,Mn,S)
For a string
Also works for matrix inputs
diffStr="4026y 4 mo 13d 9h 26m 20.145s"
pat = lookAheadBoundary("y");
newYr = "2021"; % must be a string
newstr = replaceBetween(diffStr,1,pat,newYr)
1 Kommentar
Stephen23
am 28 Feb. 2024
Without DATEVEC:
cD = calendarDuration(4026,5,13,9,26,20.145)
[~,m,d,t] = split(cD,{'years','months','days','time'})
newYr = 2021;
D = calendarDuration(newYr,m,d)+t
Star Strider
am 28 Feb. 2024
Assuming that you want to replace it with '9999' —
diffStr="4026y 4 mo 13d 9h 26m 20.145s"
val = num2str(9999)
diffStr = regexprep(diffStr, '\d*(?=y)', val)
You can of course just stubstitute in '9999' in the strrep call, however I assume that you want to do this to more than one number, (and probably in a loop, since I doubt regexprep is vectorised).
.
0 Kommentare
Voss
am 28 Feb. 2024
% calendarDuration array
d = calendarDuration(21:24,2,28,10,6,27.34).'
% string array
dStr = string(d)
% replace the years in the strings with 13
new_year = 13;
dStr = regexprep(dStr,'\d+y',sprintf('%dy',new_year))
1 Kommentar
Steven Lord
am 28 Feb. 2024
Note that this change doesn't affect d.
% calendarDuration array
d = calendarDuration(21:24,2,28,10,6,27.34).'
% string array
dStr = string(d);
% replace the years in the strings with 13
new_year = 13;
dStr = regexprep(dStr,'\d+y',sprintf('%dy',new_year))
d
If you want to change d I'd probably split the calendarDuration object into its components using datevec, modify the numeric data, and recreate the calendarDuration object.
DV = datevec(d)
DV(:, 1) = 13;
d2 = calendarDuration(DV)
Adrián László Szemenyei
am 28 Feb. 2024
If you want to change your string starting from the first character until 'y', you can use strfind with replaceBetween. If you want to "index into the string", you have to convert it to char:
diffStr="4026y 4 mo 13d 9h 26m 20.145s";
indexOfY=strfind(diffStr,'y');
diffChar=char(diffStr);
year=diffChar(1:indexOfY-1);
someValueExample=str2num(year)-100;
newDiffStr=replaceBetween(diffStr,1,indexOfY,strcat(num2str(someValueExample),"asd"))
0 Kommentare
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!