How to replace the string with certain character
Clash Royale CLAN TAG#URR8PPP
up vote
4
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 plus 2
into the file base name when the file base name is even digital. I mean I want 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
4
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 plus 2
into the file base name when the file base name is even digital. I mean I want 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
4
down vote
favorite
up vote
4
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 plus 2
into the file base name when the file base name is even digital. I mean I want 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 plus 2
into the file base name when the file base name is even digital. I mean I want 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
asked 3 hours ago
yode
9,79323097
9,79323097
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
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
2 hours ago
see here please.
â yode
2 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
2 hours ago
add a comment |Â
up vote
2
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
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.
f[str_String] := FileBaseName[str], DirectoryName[str], FileExtension[str];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[num = StringJoin[StringSplit[name,
a_ ~~ EndOfString :> ToString[FromDigits[a] + 2]]]
, FileNameJoin[dir, name <> "." <> ext]];
f[str_, __] := str
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
5 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
2 hours ago
see here please.
â yode
2 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
2 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
2 hours ago
see here please.
â yode
2 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
2 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 3 hours ago
answered 3 hours ago
kglr
160k8184384
160k8184384
Maybe more conciseStringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
2 hours ago
see here please.
â yode
2 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
2 hours ago
add a comment |Â
Maybe more conciseStringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
2 hours ago
see here please.
â yode
2 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
2 hours ago
Maybe more concise
StringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
2 hours ago
Maybe more concise
StringReplace[string, a : DigitCharacter ~~ "." /; EvenQ[FromDigits[a]] :> StringJoin[ToString[FromDigits[a] + 2]] ~~ "."]
â yode
2 hours ago
see here please.
â yode
2 hours ago
see here please.
â yode
2 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
2 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
2 hours ago
add a comment |Â
up vote
2
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
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
up vote
2
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 1 hour ago
answered 3 hours ago
Carl Woll
56.7k272147
56.7k272147
add a comment |Â
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.
f[str_String] := FileBaseName[str], DirectoryName[str], FileExtension[str];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[num = StringJoin[StringSplit[name,
a_ ~~ EndOfString :> ToString[FromDigits[a] + 2]]]
, FileNameJoin[dir, name <> "." <> ext]];
f[str_, __] := str
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
5 mins 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.
f[str_String] := FileBaseName[str], DirectoryName[str], FileExtension[str];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[num = StringJoin[StringSplit[name,
a_ ~~ EndOfString :> ToString[FromDigits[a] + 2]]]
, FileNameJoin[dir, name <> "." <> ext]];
f[str_, __] := str
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
5 mins ago
add a comment |Â
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.
f[str_String] := FileBaseName[str], DirectoryName[str], FileExtension[str];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[num = StringJoin[StringSplit[name,
a_ ~~ EndOfString :> ToString[FromDigits[a] + 2]]]
, FileNameJoin[dir, name <> "." <> ext]];
f[str_, __] := str
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.
f[str_String] := FileBaseName[str], DirectoryName[str], FileExtension[str];
f[str_, dir_, name_, ext_ /; ToExpression[name, InputForm, EvenQ]] :=
With[num = StringJoin[StringSplit[name,
a_ ~~ EndOfString :> ToString[FromDigits[a] + 2]]]
, FileNameJoin[dir, name <> "." <> ext]];
f[str_, __] := str
answered 2 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
5 mins ago
add a comment |Â
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
5 mins 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
5 mins 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
5 mins ago
add a comment |Â
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-the-string-with-certain-character%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