Extract leading numbers from a string in batch script

后端 未结 6 1635
灰色年华
灰色年华 2021-01-02 23:39

I am new to batch scripting. although i have shell and python solutions for this problem but got stuck in batch script.

I have string like 123_happy,

6条回答
  •  执念已碎
    2021-01-03 00:12

    I think this is the simplest way:

    @echo off
    setlocal
    
    set "str=123_happy"
    set /A num=str
    echo Result = %num%
    

    When set /A command get a value from a variable, it convert the number until the first non-digit character with no error.

    To preserve left zeros:

    set "str=0128_happy"
    set "num=1%str%"
    set /A num=num
    set "num=%num:~1%"
    echo Result = %num%
    

提交回复
热议问题