user1912842
user1912842

Reputation: 43

Retrieve a variable length substring based on a delimiting character

I'm tryin to make a simple command to strip everything after a word, and echo the result.

Example:

street-ct-2a21340565364563563
thiswouldbedifferent-ss-3c63456345645635634
andthiscouldbesomethingelse-we-5d23453453634563456

removing everything expect the first section

Something like:

street-ct-221340565364563563

would result in:

street

This isn't being saved in a txt file or anything, is it possible?

Upvotes: 4

Views: 4263

Answers (1)

fresskoma
fresskoma

Reputation: 25781

Edit: Assuming that the part you are trying to extract has variable length, this seems to be an even better solution:

set str=street-ct-2a21340565364563563
for /f "delims=-" %%a in ("%str%") do set part=%%a
echo.%part%

In that snippet, we split the string whenever a "-" occurs, and then assign the first part of the split string to the "part" variable. Additional examples on how to use for to split strings can be found here.


According to this reference you could do something like this:

set str=street-ct-2a21340565364563563
set substr=%str:~0,6%
echo.%substr%

Upvotes: 6

Related Questions