0

So I want a program like this:

Enter number: 
5
Giving first 5 characters of string "apple pie"...
apple

I have tried something like this

set characters=5
set "string=apple pie"

set string=%string:~0, %characters%%

But it's not working, any idea why?

0

1 Answer 1

2

either rewrite

set string=%string:~0, %characters%%

as

call set "string=%%string:~0, %characters%%%"

or use delayed expansion

@echo off
set characters=5
set "string=apple pie"

setlocal enabledelayedexpansion
set "string=!string:~0, %characters%!"
echo %string%

see Variables in batch not behaving as expected

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This worked very well, I did try use delayed expansion but I couldn't figure out the exclamation marks.
IMHO the referred link does not talk about the use of !this:~0,%combination%!, but just about Delayed Expansion. See this answer instead that describe precisely this management, although the topic is different (arrays).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.