1.

Solve : Extract contents from a string?

Answer»

I have a variable string that looks like the following:
            Code: [Select]SET VAR1="could be anything++++something+anything can follow+something+something"How do I EXTRACT everything from the "++++" to the end of the string?  In this case, my RESULT would be:  ++++something+anything can follow+something+something.
The string is not fixed in size, the key is extracting everything from ++++ to the end of the string.

I did the following, but then that is cheating:
          Code: [Select]SET VAR2=%VAR1:*++++="++++%Is the separator between sections always 4 plus signs?

Is it always the first occurrence in the string of that character?The 4 plus signs may or may not appear in a string.  In my script, I do test for 4 plus signs before continuing else I will exit.
If the 4 plus signs does appear, I WANT to extract everything from the 4 plus signs until the end of the string.If a string does not contain 4 plus signs, it is ignored?

If a string does contain 4 plus signs, are they the first occurence of plus signs in the string?

Do you want any later plus signs to remain?


If a string does not contain 4 plus signs, it is ignored?    Yes, it is ignored if there are not 4 plus signs.

If a string does contain 4 plus signs, are they the first occurence of plus signs in the string?  Anything can precede the 4 plus signs, I am interested only in the contents from the start of the 4 plus signs to the very end of the string.

Do you want any later plus signs to remain?  All plus signs must remain, see below:.

If the string is:                                "could be anything++++something+anything can follow+something+something"
I want the following returned:        "++++something+anything can follow+something+something"You can use FOR /F to split the string into two halves, the part before ++++ and the part after, and then PUT back the ++++.

echo off
SET VAR1="could be anything++++something+anything can follow+something+something"
for /f "tokens=1* delims=+" %%A in (%var1%) do (
    set firstpart=%%A
    set lastpart=++++%%B
   )
echo whole string %var1%
echo first part   %firstpart%
echo last  part   %lastpart%
echo.
pause

Output...

C:\Batch\Test>test1.bat
whole string "could be anything++++something+anything can follow+something+something"
first part   could be anything
last  part   ++++something+anything can follow+something+something

Press any key to continue . . .


Works like a charm, thank you very much.....



Discussion

No Comment Found