How to replace numbers in a string?
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I have a long string list:
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png"
Now, I want to add 2 to the file name base when the file base name is an even number. I mean, I hope to get such new string list:
string = "E:\job\a\000251.png", "E:\job\a\000254.png",
"E:\job\a\000253.png", "E:\job\a\000256.png",
"E:\job\a\000255.png", "E:\job\a\000258.png"
How to implement it?
string-manipulation stringreplace
add a comment |Â
up vote
5
down vote
favorite
I have a long string list:
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png"
Now, I want to add 2 to the file name base when the file base name is an even number. I mean, I hope to get such new string list:
string = "E:\job\a\000251.png", "E:\job\a\000254.png",
"E:\job\a\000253.png", "E:\job\a\000256.png",
"E:\job\a\000255.png", "E:\job\a\000258.png"
How to implement it?
string-manipulation stringreplace
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have a long string list:
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png"
Now, I want to add 2 to the file name base when the file base name is an even number. I mean, I hope to get such new string list:
string = "E:\job\a\000251.png", "E:\job\a\000254.png",
"E:\job\a\000253.png", "E:\job\a\000256.png",
"E:\job\a\000255.png", "E:\job\a\000258.png"
How to implement it?
string-manipulation stringreplace
I have a long string list:
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png"
Now, I want to add 2 to the file name base when the file base name is an even number. I mean, I hope to get such new string list:
string = "E:\job\a\000251.png", "E:\job\a\000254.png",
"E:\job\a\000253.png", "E:\job\a\000256.png",
"E:\job\a\000255.png", "E:\job\a\000258.png"
How to implement it?
string-manipulation stringreplace
string-manipulation stringreplace
edited 6 mins ago
Johu
2,776929
2,776929
asked 6 hours ago
yode
9,79823097
9,79823097
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
You could create a helper function (the point of the helper function is to avoid calling FromDigits
twice):
incString[s_] := With[r = FromDigits[s],
If[EvenQ[r],
IntegerString[r+2, 10, StringLength[s]],
s
]
]
and then use this helper function in StringReplace
:
StringReplace[
string,
i:DigitCharacter..~~".png" :> incString[i]<>".png"
]
"E:joba00251.png", "E:joba00254.png",
"E:joba00253.png", "E:joba00256.png", "E:joba00255.png",
"E:joba00258.png"
add a comment |Â
up vote
2
down vote
StringReplace[string,
a : NumberString ~~ "." /; EvenQ[FromDigits[a]] :>
StringJoin[ToString /@
PadLeft[IntegerDigits[FromDigits[a] + 2], StringLength@a]] ~~ "."]
"E:joba00251.png",
"E:joba00254.png",
"E:joba00253.png",
"E:joba00256.png",
"E:joba00255.png",
"E:joba00258.png"
Maybe more conciseStringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
5 hours ago
see here please.
â yode
5 hours ago
@yode, if you don't have elements like"E:joba00258.png"
in your string list your more concise version would work too.
â kglr
5 hours ago
add a comment |Â
up vote
0
down vote
Here is an approach without StringReplace
. It is one function f
that first destructures the file-name and has two more definitions: one for files with an even number and one for all others.
ClearAll[f]
f[str_String] := f[str, DirectoryName[str], FileBaseName[str], FileExtension[str]];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[
num = IntegerString[FromDigits[name] + 2, 10, StringLength[name]]
, FileNameJoin[dir, num <> "." <> ext]];
f[str_, __] := str
Now better test it properly since my last version was completely screwed up. I need to make Linux filenames from your strings since otherwise, Mathematica's file functions don't work here. On your Windows machine, this should not be necessary.
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png";
string = StringReplace[string, "E:\" :> "/", "\" :> "/"]
f/@string//Column
(*
/job/a/000251.png
/job/a/000254.png
/job/a/000253.png
/job/a/000256.png
/job/a/000255.png
/job/a/000258.png
*)
I have the greatest admiration for your contributions. I am trying to follow your answer but I believe there is an error in the second definition off
.num
is defined as a local constant inWith
but never used in the output. I would also appreciate it if you would show how to usef
for the given example.
â Jack LaVigne
2 hours ago
@JackLaVigne Oh my goodness. There was so much wrong in my code because I didn't clearf
for testing. Thank you for paying attention! Would you test if it works on windows with the original definition ofstrings
?
â halirutanâ¦
2 hours ago
Works fine in Windows. I was thinking I had toMap
the functionf`` over
string`. Thank you for the update and example. It is clear to me now.
â Jack LaVigne
2 hours ago
It doesn't work for "/job/a/000258.png" wheref
returns "/job/a/0002510.png".
â Carl Woll
2 hours ago
@CarlWoll If you are on Windows, then this seems to be expected. Filename separators are OS specific and Mathematica's functions don't work if you are on Windows and try Unix files.
â halirutanâ¦
2 hours ago
 |Â
show 3 more comments
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You could create a helper function (the point of the helper function is to avoid calling FromDigits
twice):
incString[s_] := With[r = FromDigits[s],
If[EvenQ[r],
IntegerString[r+2, 10, StringLength[s]],
s
]
]
and then use this helper function in StringReplace
:
StringReplace[
string,
i:DigitCharacter..~~".png" :> incString[i]<>".png"
]
"E:joba00251.png", "E:joba00254.png",
"E:joba00253.png", "E:joba00256.png", "E:joba00255.png",
"E:joba00258.png"
add a comment |Â
up vote
3
down vote
You could create a helper function (the point of the helper function is to avoid calling FromDigits
twice):
incString[s_] := With[r = FromDigits[s],
If[EvenQ[r],
IntegerString[r+2, 10, StringLength[s]],
s
]
]
and then use this helper function in StringReplace
:
StringReplace[
string,
i:DigitCharacter..~~".png" :> incString[i]<>".png"
]
"E:joba00251.png", "E:joba00254.png",
"E:joba00253.png", "E:joba00256.png", "E:joba00255.png",
"E:joba00258.png"
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You could create a helper function (the point of the helper function is to avoid calling FromDigits
twice):
incString[s_] := With[r = FromDigits[s],
If[EvenQ[r],
IntegerString[r+2, 10, StringLength[s]],
s
]
]
and then use this helper function in StringReplace
:
StringReplace[
string,
i:DigitCharacter..~~".png" :> incString[i]<>".png"
]
"E:joba00251.png", "E:joba00254.png",
"E:joba00253.png", "E:joba00256.png", "E:joba00255.png",
"E:joba00258.png"
You could create a helper function (the point of the helper function is to avoid calling FromDigits
twice):
incString[s_] := With[r = FromDigits[s],
If[EvenQ[r],
IntegerString[r+2, 10, StringLength[s]],
s
]
]
and then use this helper function in StringReplace
:
StringReplace[
string,
i:DigitCharacter..~~".png" :> incString[i]<>".png"
]
"E:joba00251.png", "E:joba00254.png",
"E:joba00253.png", "E:joba00256.png", "E:joba00255.png",
"E:joba00258.png"
edited 4 hours ago
answered 5 hours ago
Carl Woll
56.8k272147
56.8k272147
add a comment |Â
add a comment |Â
up vote
2
down vote
StringReplace[string,
a : NumberString ~~ "." /; EvenQ[FromDigits[a]] :>
StringJoin[ToString /@
PadLeft[IntegerDigits[FromDigits[a] + 2], StringLength@a]] ~~ "."]
"E:joba00251.png",
"E:joba00254.png",
"E:joba00253.png",
"E:joba00256.png",
"E:joba00255.png",
"E:joba00258.png"
Maybe more conciseStringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
5 hours ago
see here please.
â yode
5 hours ago
@yode, if you don't have elements like"E:joba00258.png"
in your string list your more concise version would work too.
â kglr
5 hours ago
add a comment |Â
up vote
2
down vote
StringReplace[string,
a : NumberString ~~ "." /; EvenQ[FromDigits[a]] :>
StringJoin[ToString /@
PadLeft[IntegerDigits[FromDigits[a] + 2], StringLength@a]] ~~ "."]
"E:joba00251.png",
"E:joba00254.png",
"E:joba00253.png",
"E:joba00256.png",
"E:joba00255.png",
"E:joba00258.png"
Maybe more conciseStringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
5 hours ago
see here please.
â yode
5 hours ago
@yode, if you don't have elements like"E:joba00258.png"
in your string list your more concise version would work too.
â kglr
5 hours ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
StringReplace[string,
a : NumberString ~~ "." /; EvenQ[FromDigits[a]] :>
StringJoin[ToString /@
PadLeft[IntegerDigits[FromDigits[a] + 2], StringLength@a]] ~~ "."]
"E:joba00251.png",
"E:joba00254.png",
"E:joba00253.png",
"E:joba00256.png",
"E:joba00255.png",
"E:joba00258.png"
StringReplace[string,
a : NumberString ~~ "." /; EvenQ[FromDigits[a]] :>
StringJoin[ToString /@
PadLeft[IntegerDigits[FromDigits[a] + 2], StringLength@a]] ~~ "."]
"E:joba00251.png",
"E:joba00254.png",
"E:joba00253.png",
"E:joba00256.png",
"E:joba00255.png",
"E:joba00258.png"
edited 5 hours ago
answered 5 hours ago
kglr
160k8184384
160k8184384
Maybe more conciseStringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
5 hours ago
see here please.
â yode
5 hours ago
@yode, if you don't have elements like"E:joba00258.png"
in your string list your more concise version would work too.
â kglr
5 hours ago
add a comment |Â
Maybe more conciseStringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
5 hours ago
see here please.
â yode
5 hours ago
@yode, if you don't have elements like"E:joba00258.png"
in your string list your more concise version would work too.
â kglr
5 hours ago
Maybe more concise
StringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
5 hours ago
Maybe more concise
StringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
5 hours ago
see here please.
â yode
5 hours ago
see here please.
â yode
5 hours ago
@yode, if you don't have elements like
"E:joba00258.png"
in your string list your more concise version would work too.â kglr
5 hours ago
@yode, if you don't have elements like
"E:joba00258.png"
in your string list your more concise version would work too.â kglr
5 hours ago
add a comment |Â
up vote
0
down vote
Here is an approach without StringReplace
. It is one function f
that first destructures the file-name and has two more definitions: one for files with an even number and one for all others.
ClearAll[f]
f[str_String] := f[str, DirectoryName[str], FileBaseName[str], FileExtension[str]];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[
num = IntegerString[FromDigits[name] + 2, 10, StringLength[name]]
, FileNameJoin[dir, num <> "." <> ext]];
f[str_, __] := str
Now better test it properly since my last version was completely screwed up. I need to make Linux filenames from your strings since otherwise, Mathematica's file functions don't work here. On your Windows machine, this should not be necessary.
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png";
string = StringReplace[string, "E:\" :> "/", "\" :> "/"]
f/@string//Column
(*
/job/a/000251.png
/job/a/000254.png
/job/a/000253.png
/job/a/000256.png
/job/a/000255.png
/job/a/000258.png
*)
I have the greatest admiration for your contributions. I am trying to follow your answer but I believe there is an error in the second definition off
.num
is defined as a local constant inWith
but never used in the output. I would also appreciate it if you would show how to usef
for the given example.
â Jack LaVigne
2 hours ago
@JackLaVigne Oh my goodness. There was so much wrong in my code because I didn't clearf
for testing. Thank you for paying attention! Would you test if it works on windows with the original definition ofstrings
?
â halirutanâ¦
2 hours ago
Works fine in Windows. I was thinking I had toMap
the functionf`` over
string`. Thank you for the update and example. It is clear to me now.
â Jack LaVigne
2 hours ago
It doesn't work for "/job/a/000258.png" wheref
returns "/job/a/0002510.png".
â Carl Woll
2 hours ago
@CarlWoll If you are on Windows, then this seems to be expected. Filename separators are OS specific and Mathematica's functions don't work if you are on Windows and try Unix files.
â halirutanâ¦
2 hours ago
 |Â
show 3 more comments
up vote
0
down vote
Here is an approach without StringReplace
. It is one function f
that first destructures the file-name and has two more definitions: one for files with an even number and one for all others.
ClearAll[f]
f[str_String] := f[str, DirectoryName[str], FileBaseName[str], FileExtension[str]];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[
num = IntegerString[FromDigits[name] + 2, 10, StringLength[name]]
, FileNameJoin[dir, num <> "." <> ext]];
f[str_, __] := str
Now better test it properly since my last version was completely screwed up. I need to make Linux filenames from your strings since otherwise, Mathematica's file functions don't work here. On your Windows machine, this should not be necessary.
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png";
string = StringReplace[string, "E:\" :> "/", "\" :> "/"]
f/@string//Column
(*
/job/a/000251.png
/job/a/000254.png
/job/a/000253.png
/job/a/000256.png
/job/a/000255.png
/job/a/000258.png
*)
I have the greatest admiration for your contributions. I am trying to follow your answer but I believe there is an error in the second definition off
.num
is defined as a local constant inWith
but never used in the output. I would also appreciate it if you would show how to usef
for the given example.
â Jack LaVigne
2 hours ago
@JackLaVigne Oh my goodness. There was so much wrong in my code because I didn't clearf
for testing. Thank you for paying attention! Would you test if it works on windows with the original definition ofstrings
?
â halirutanâ¦
2 hours ago
Works fine in Windows. I was thinking I had toMap
the functionf`` over
string`. Thank you for the update and example. It is clear to me now.
â Jack LaVigne
2 hours ago
It doesn't work for "/job/a/000258.png" wheref
returns "/job/a/0002510.png".
â Carl Woll
2 hours ago
@CarlWoll If you are on Windows, then this seems to be expected. Filename separators are OS specific and Mathematica's functions don't work if you are on Windows and try Unix files.
â halirutanâ¦
2 hours ago
 |Â
show 3 more comments
up vote
0
down vote
up vote
0
down vote
Here is an approach without StringReplace
. It is one function f
that first destructures the file-name and has two more definitions: one for files with an even number and one for all others.
ClearAll[f]
f[str_String] := f[str, DirectoryName[str], FileBaseName[str], FileExtension[str]];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[
num = IntegerString[FromDigits[name] + 2, 10, StringLength[name]]
, FileNameJoin[dir, num <> "." <> ext]];
f[str_, __] := str
Now better test it properly since my last version was completely screwed up. I need to make Linux filenames from your strings since otherwise, Mathematica's file functions don't work here. On your Windows machine, this should not be necessary.
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png";
string = StringReplace[string, "E:\" :> "/", "\" :> "/"]
f/@string//Column
(*
/job/a/000251.png
/job/a/000254.png
/job/a/000253.png
/job/a/000256.png
/job/a/000255.png
/job/a/000258.png
*)
Here is an approach without StringReplace
. It is one function f
that first destructures the file-name and has two more definitions: one for files with an even number and one for all others.
ClearAll[f]
f[str_String] := f[str, DirectoryName[str], FileBaseName[str], FileExtension[str]];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[
num = IntegerString[FromDigits[name] + 2, 10, StringLength[name]]
, FileNameJoin[dir, num <> "." <> ext]];
f[str_, __] := str
Now better test it properly since my last version was completely screwed up. I need to make Linux filenames from your strings since otherwise, Mathematica's file functions don't work here. On your Windows machine, this should not be necessary.
string = "E:\job\a\000251.png", "E:\job\a\000252.png",
"E:\job\a\000253.png", "E:\job\a\000254.png",
"E:\job\a\000255.png", "E:\job\a\000256.png";
string = StringReplace[string, "E:\" :> "/", "\" :> "/"]
f/@string//Column
(*
/job/a/000251.png
/job/a/000254.png
/job/a/000253.png
/job/a/000256.png
/job/a/000255.png
/job/a/000258.png
*)
edited 2 hours ago
answered 4 hours ago
halirutanâ¦
92.8k5212405
92.8k5212405
I have the greatest admiration for your contributions. I am trying to follow your answer but I believe there is an error in the second definition off
.num
is defined as a local constant inWith
but never used in the output. I would also appreciate it if you would show how to usef
for the given example.
â Jack LaVigne
2 hours ago
@JackLaVigne Oh my goodness. There was so much wrong in my code because I didn't clearf
for testing. Thank you for paying attention! Would you test if it works on windows with the original definition ofstrings
?
â halirutanâ¦
2 hours ago
Works fine in Windows. I was thinking I had toMap
the functionf`` over
string`. Thank you for the update and example. It is clear to me now.
â Jack LaVigne
2 hours ago
It doesn't work for "/job/a/000258.png" wheref
returns "/job/a/0002510.png".
â Carl Woll
2 hours ago
@CarlWoll If you are on Windows, then this seems to be expected. Filename separators are OS specific and Mathematica's functions don't work if you are on Windows and try Unix files.
â halirutanâ¦
2 hours ago
 |Â
show 3 more comments
I have the greatest admiration for your contributions. I am trying to follow your answer but I believe there is an error in the second definition off
.num
is defined as a local constant inWith
but never used in the output. I would also appreciate it if you would show how to usef
for the given example.
â Jack LaVigne
2 hours ago
@JackLaVigne Oh my goodness. There was so much wrong in my code because I didn't clearf
for testing. Thank you for paying attention! Would you test if it works on windows with the original definition ofstrings
?
â halirutanâ¦
2 hours ago
Works fine in Windows. I was thinking I had toMap
the functionf`` over
string`. Thank you for the update and example. It is clear to me now.
â Jack LaVigne
2 hours ago
It doesn't work for "/job/a/000258.png" wheref
returns "/job/a/0002510.png".
â Carl Woll
2 hours ago
@CarlWoll If you are on Windows, then this seems to be expected. Filename separators are OS specific and Mathematica's functions don't work if you are on Windows and try Unix files.
â halirutanâ¦
2 hours ago
I have the greatest admiration for your contributions. I am trying to follow your answer but I believe there is an error in the second definition of
f
. num
is defined as a local constant in With
but never used in the output. I would also appreciate it if you would show how to use f
for the given example.â Jack LaVigne
2 hours ago
I have the greatest admiration for your contributions. I am trying to follow your answer but I believe there is an error in the second definition of
f
. num
is defined as a local constant in With
but never used in the output. I would also appreciate it if you would show how to use f
for the given example.â Jack LaVigne
2 hours ago
@JackLaVigne Oh my goodness. There was so much wrong in my code because I didn't clear
f
for testing. Thank you for paying attention! Would you test if it works on windows with the original definition of strings
?â halirutanâ¦
2 hours ago
@JackLaVigne Oh my goodness. There was so much wrong in my code because I didn't clear
f
for testing. Thank you for paying attention! Would you test if it works on windows with the original definition of strings
?â halirutanâ¦
2 hours ago
Works fine in Windows. I was thinking I had to
Map
the function f`` over
string`. Thank you for the update and example. It is clear to me now.â Jack LaVigne
2 hours ago
Works fine in Windows. I was thinking I had to
Map
the function f`` over
string`. Thank you for the update and example. It is clear to me now.â Jack LaVigne
2 hours ago
It doesn't work for "/job/a/000258.png" where
f
returns "/job/a/0002510.png".â Carl Woll
2 hours ago
It doesn't work for "/job/a/000258.png" where
f
returns "/job/a/0002510.png".â Carl Woll
2 hours ago
@CarlWoll If you are on Windows, then this seems to be expected. Filename separators are OS specific and Mathematica's functions don't work if you are on Windows and try Unix files.
â halirutanâ¦
2 hours ago
@CarlWoll If you are on Windows, then this seems to be expected. Filename separators are OS specific and Mathematica's functions don't work if you are on Windows and try Unix files.
â halirutanâ¦
2 hours ago
 |Â
show 3 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181917%2fhow-to-replace-numbers-in-a-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password