How to replace the string with certain character

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
4
down vote

favorite
1












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?










share|improve this question

























    up vote
    4
    down vote

    favorite
    1












    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?










    share|improve this question























      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      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?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 3 hours ago









      yode

      9,79323097




      9,79323097




















          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"







          share|improve this answer






















          • 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










          • @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

















          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"







          share|improve this answer





























            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





            share|improve this answer




















            • 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










            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "387"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            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






























            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"







            share|improve this answer






















            • 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










            • @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














            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"







            share|improve this answer






















            • 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










            • @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












            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"







            share|improve this answer














            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"








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 3 hours ago

























            answered 3 hours ago









            kglr

            160k8184384




            160k8184384











            • 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










            • @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










            • 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










            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"







            share|improve this answer


























              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"







              share|improve this answer
























                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"







                share|improve this answer














                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"








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago

























                answered 3 hours ago









                Carl Woll

                56.7k272147




                56.7k272147




















                    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





                    share|improve this answer




















                    • 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














                    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





                    share|improve this answer




















                    • 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












                    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





                    share|improve this answer












                    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






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    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 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















                    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

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    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













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke