Replace Before A Specific Character

7 Ansichten (letzte 30 Tage)
CHRISTY
CHRISTY am 28 Feb. 2024
Kommentiert: Stephen23 am 28 Feb. 2024
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
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.

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Cris LaPierre
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)
d = calendarDuration
4026y 5mo 13d 9h 26m 20.145s
[Y,M,D,H,Mn,S] = datevec(d);
newYr = 2021;
D = calendarDuration(newYr,M,D,H,Mn,S)
D = calendarDuration
2021y 5mo 13d 9h 26m 20.145s
For a string
Also works for matrix inputs
diffStr="4026y 4 mo 13d 9h 26m 20.145s"
diffStr = "4026y 4 mo 13d 9h 26m 20.145s"
pat = lookAheadBoundary("y");
newYr = "2021"; % must be a string
newstr = replaceBetween(diffStr,1,pat,newYr)
newstr = "2021y 4 mo 13d 9h 26m 20.145s"
  1 Kommentar
Stephen23
Stephen23 am 28 Feb. 2024
Without DATEVEC:
cD = calendarDuration(4026,5,13,9,26,20.145)
cD = calendarDuration
4026y 5mo 13d 9h 26m 20.145s
[~,m,d,t] = split(cD,{'years','months','days','time'})
m = 5
d = 13
t = duration
09:26:20
newYr = 2021;
D = calendarDuration(newYr,m,d)+t
D = calendarDuration
2021y 5mo 13d 9h 26m 20.145s

Melden Sie sich an, um zu kommentieren.


Star Strider
Star Strider am 28 Feb. 2024
Assuming that you want to replace it with '9999'
diffStr="4026y 4 mo 13d 9h 26m 20.145s"
diffStr = "4026y 4 mo 13d 9h 26m 20.145s"
val = num2str(9999)
val = '9999'
diffStr = regexprep(diffStr, '\d*(?=y)', val)
diffStr = "9999y 4 mo 13d 9h 26m 20.145s"
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).
.

Voss
Voss am 28 Feb. 2024
% calendarDuration array
d = calendarDuration(21:24,2,28,10,6,27.34).'
d = 4×1 calendarDuration array
21y 2mo 28d 10h 6m 27.34s 22y 2mo 28d 10h 6m 27.34s 23y 2mo 28d 10h 6m 27.34s 24y 2mo 28d 10h 6m 27.34s
% string array
dStr = string(d)
dStr = 4×1 string array
"21y 2mo 28d 10h 6m 27.34s" "22y 2mo 28d 10h 6m 27.34s" "23y 2mo 28d 10h 6m 27.34s" "24y 2mo 28d 10h 6m 27.34s"
% replace the years in the strings with 13
new_year = 13;
dStr = regexprep(dStr,'\d+y',sprintf('%dy',new_year))
dStr = 4×1 string array
"13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s"
  1 Kommentar
Steven Lord
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).'
d = 4×1 calendarDuration array
21y 2mo 28d 10h 6m 27.34s 22y 2mo 28d 10h 6m 27.34s 23y 2mo 28d 10h 6m 27.34s 24y 2mo 28d 10h 6m 27.34s
% 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))
dStr = 4×1 string array
"13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s" "13y 2mo 28d 10h 6m 27.34s"
d
d = 4×1 calendarDuration array
21y 2mo 28d 10h 6m 27.34s 22y 2mo 28d 10h 6m 27.34s 23y 2mo 28d 10h 6m 27.34s 24y 2mo 28d 10h 6m 27.34s
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 = 4×6
21.0000 2.0000 28.0000 10.0000 6.0000 27.3400 22.0000 2.0000 28.0000 10.0000 6.0000 27.3400 23.0000 2.0000 28.0000 10.0000 6.0000 27.3400 24.0000 2.0000 28.0000 10.0000 6.0000 27.3400
DV(:, 1) = 13;
d2 = calendarDuration(DV)
d2 = 4×1 calendarDuration array
13y 2mo 28d 10h 6m 27.34s 13y 2mo 28d 10h 6m 27.34s 13y 2mo 28d 10h 6m 27.34s 13y 2mo 28d 10h 6m 27.34s

Melden Sie sich an, um zu kommentieren.


Adrián László Szemenyei
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"))
newDiffStr = "3926asd 4 mo 13d 9h 26m 20.145s"

Tags

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by