MoonPoint Support Logo

 


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
February
Sun Mon Tue Wed Thu Fri Sat
14
2009
Months
Feb


Sat, Feb 14, 2009 8:24 pm

Extracting a Substring from a String under Microsoft Windows

If you are using Microsoft Windows, such as Windows XP, you can extract a substring from a variable string using character positional notation, i.e. %myvar:~char_skip_num%. I.e., where you have some variable, e.g. %myvar%, you can place the :~ operator prior to the ending % and then specify a the number of characters to skip followed by the ending %.

E.g. from a command line if you checked the value of a user profile variable, i.e. %userprofile%, for a user named James, you might see the following:

C:\>echo %userprofile%
C:\Documents and Settings\James

If I wanted to extract just the user's account name, i.e. James, I could use the following:

C:\>echo %userprofile:~26%
James

The "J" in James is the 27th character, so I used 26 to have the first 26 characters skipped.

You can also specify that you only want to extract a certain number of characters by putting a comma after the number of characters to be skipped followed by the number of characters to extract. E.g, if I only wanted to extract the first 3 characters starting at position 27, I could use the following:

C:\>echo %userprofile:~26,3%
Jam

If you don't want to skip any characters, but specify only a certain number of characters to extract, you can use the syntax %myvar:~0,x where you specify that zero characters are to be skipped and x represents the number of characters to extract from the string.

C:\>echo %userprofile:~0,3%
C:\

You can also specify that you want to start the extraction from the end of the line rather than the beginning by using -x, where x is some number, for the starting position. E.g. you could use the following to extract the last 3 characters from the line:

C:\>echo %userprofile:~-3%
mes

You can specify the number of characters to extract, just as noted before, with this method as well. E.g. to extract the substring that starts at 3 characters from the end of the string, but only includes 2 characters from that point, the following could be used:

C:\>echo %userprofile:~-3,2%
me

If you are using a batch file, e.g. substring-extract-example.bat, you could display just the desired part of the string as follows:

@echo off
echo %userprofile:~-3,2%

When the batch file is executed, it wold display just "me"

C:\>substr-extract-example
me

For further examples, see Variables: extract part of a variable (substring)

References:

  1. Variables: extract part of a variable (substring)
    SS64.com
  2. Substrings in Windows Batch Files
    Terminally Incoherent

[/os/windows/commands] permanent link

Once You Know, You Newegg AliExpress by Alibaba.com

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo