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?

1 Answers1

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

Community
  • 1
  • 1
elzooilogico
  • 1,659
  • 2
  • 17
  • 18
  • Thank you! This worked very well, I did try use delayed expansion but I couldn't figure out the exclamation marks. –  May 10 '17 at 09:54
  • IMHO the referred link does not talk about the use of `!this:~0,%combination%!`, but just about Delayed Expansion. See [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) instead that describe precisely this management, although the topic is different (arrays). – Aacini May 10 '17 at 13:28