Is it possible to use variables in a substring operation?
Posted: 2021-Nov-08, 2:08 am
I want to do something like this;
Where the substring operation would return however many characters the user enters. If they enter "5", they'd get "12345".
I've tried it using Delayed Expansion and that didn't seem to work either.
I can use a For token in the substring, and I've figured out a work-around for what I want to do, but it would be much simpler if there was a way to directly use a variable.
Is this possible?
EDIT:
After much experimentation, it seems that enabling delayed expansion and using !String:~0,%x%! works.
Code: Select all
set String=1234567890
set /p x=Number of desired characters?
echo %String:~0,%x%%
I've tried it using Delayed Expansion and that didn't seem to work either.
I can use a For token in the substring, and I've figured out a work-around for what I want to do, but it would be much simpler if there was a way to directly use a variable.
Is this possible?
EDIT:
After much experimentation, it seems that enabling delayed expansion and using !String:~0,%x%! works.