Replace Before A Specific Character
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2024-2-28
编辑:Stephen23
2024-2-28
"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.
回答(4 个)
Cris LaPierre
2024-2-28
编辑:Cris LaPierre
2024-2-28
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 个评论
Stephen23
2024-2-28
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
2024-2-28
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 个评论
Voss
2024-2-28
% 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 个评论
Steven Lord
2024-2-28
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
2024-2-28
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!