0

I have a batch file that helps me with string manipulation. It works as a plugin/function. The problem is, I don't know how do I use variables in string manipulation.

Here is the code that i have problem with:

set result=%_unstring:%_charstoreplace%=%_replacementchars%

The variables _unstring, _charstoreplace and _replacementchars are all parameters.

How do I use variables while manipulating %_unstring%?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    Instead of entering into conversations after people have provided an answer for you, why not provide the exact code that is giving you an issue, and ask a specific question. As it stands your question is not representative of a reproducible problem, unclear and too difficult for us to help you with in a succinct manner. Please use the [edit facility](https://stackoverflow.com/posts/49609311/edit), to update your question accordingly. – Compo Apr 02 '18 at 11:14
  • Just use `set result=!_unstring:%_charstoreplace%=%_replacementchars%!` and put a `setlocal EnableDelayedExpansion` command at beginning. This method is fully explained at [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) (although the topic is different). – Aacini Apr 02 '18 at 15:42
  • But why after this command i do `echo %result%` and it outputs `ECHO is off.`? –  Apr 07 '18 at 19:57

1 Answers1

1
SET "unstring=some string containing stuff"
SET "charstoreplace=ing cont"
SET "replacementchars=with this"

CALL SET "result=%%unstring:%charstoreplace%=%replacementchars%%%"
SET result

is probably the easiest. It relies on the parsing method used by batch.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • It works but it only prints it out. like `result=Hi world` and it does not store it in the result variable. Also i would like it to not go one line down after executing. Any ideas? –  Apr 02 '18 at 10:33
  • 1
    @DatProgrammer - the result is stored in the variable `%result%`. The last `set` was used instead of `echo` to display the variable. – SomethingDark Apr 02 '18 at 12:57