Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:58:00+00:00 2026-06-18T02:58:00+00:00

I want to split a string in two parts, without using any for loop.

  • 0

I want to split a string in two parts, without using any for loop.

For example, I have the string in a variable:

str=45:abc

I want to get 45 in a variable and abc in another variable. Is it possible in batch file?

pattern is like somenumber:somestring

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:58:01+00:00Added an answer on June 18, 2026 at 2:58 am

    You could split the str with different ways.

    The for loop, you don’t want use it.

    The trailing part is easy with the * (match anything until …)
    set "var2=%str:*:=%"

    The leading part can be done with a nasty trick
    set "var1=%str::="^&REM #%

    The caret is needed to escape the ampersand,
    so effectivly the colon will be replaced by "&REM #
    So in your case you got the line after replacing
    set "var1=4567"&REM #abcde
    And this is splitted into two commands

    set "var1=4567"
    REM #abcde` 
    

    And the complete code is here:

    set "str=4567:abcde"
    echo %str%
    set "var1=%str::="^&REM #%
    set "var2=%str:*:=%"
    echo var1=%var1% var2=%var2%
    

    Edit 2: More stable leading part

    Thanks Dave for the idea to use a linefeed.
    The REM technic isn’t very stable against content with quotes and special characters.
    But with a linefeed trick there exists a more stable version which also works when the split argument is longer than a single character.

    @echo off
    setlocal enableDelayedExpansion
    set ^"str=456789#$#abc"
    for /F "delims=" %%a in (^"!str:#$#^=^
    
    !^") do (
      set "lead=%%a"
      goto :break
    )
    :break
    echo !lead!
    

    Solution 3: Adpated dbenhams answer

    Dbenham uses in his solution a linefeed with a pipe.
    This seems a bit over complicated.
    As the solution uses the fact, that the parser removes the rest of the line after an unescaped linefeed (when this is found before or in the special character phase).

    At first the colon character is replaced to a linefeed with delayed expansion replacement.
    That is allowed and the linefeed is now part of the variable.
    Then the line set lead=%lead% strips the trailing part.
    It’s better not to use the extended syntax here, as set "lead=%lead%" would break if a quote is part of the string.

    setlocal enableDelayedExpansion
    set "str=45:abc"
    set ^"lead=!str::=^
    
    !"
    set lead=%lead%
    echo "!lead!"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split following string into two parts using split function of javascript
I want to split a string in Javascript using split function into 2 parts.
I want to split a string using a delimiter, for example split "004-034556" into
I am using the String split method and I want to have the last
I want to split a string into two parts, the string is almost free
I want to split a string into only two parts at the first space
I want to split a string using regular expressions but I have run into
i'm amol kadam,I want to know how to split string in two part.My string
I want to split a string like this: abc//def//ghi into a part before and
I want to split a string with all non-alphabetic characters as delimiters. For example,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.