Hi all!
I can't find answer to the following question: How can I extract only the last word from a string?
Thanks
Extract only the last word from a string
Moderator: DosItHelp
Re: Extract only the last word from a string
Here is one way:
If you want to do something else then give us full details of the task.
This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
If you want to do something else then give us full details of the task.
Code: Select all
@echo off
set "string=A cat and dog had a quarrel"
echo "%string%"|repl ".* (.*)." "$1"
pause
This uses a helper batch file called `repl.bat` (by dbenham) - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place `repl.bat` in the same folder as the batch file or in a folder that is on the path.
Re: Extract only the last word from a string
Code: Select all
@echo off
set string=Give me the last word in this sentence
FOR %%A IN (%string%) DO SET last=%%A
echo %last%
pause
Re: Extract only the last word from a string
Put an ampersand in there and BANG!
Re: Extract only the last word from a string
foxidrive wrote:Put an ampersand in there and BANG!
Without the Bing Bang:
Code: Select all
@echo off
setlocal enabledelayedexpansion
set "string=|| <> :&"
for /f "delims=" %%i in ('cmd /u /c set /p"=%string%"^<nul^| more^| findstr /n "^"') do (
set symbol=%%i
if not "!symbol:*:=!"==" " (set last=!last!!symbol:*:=!) else (set last=)
)
for /f "eol=" %%i in ("%last%") do echo %%i
endlocal
pause>nul
exit /b
.
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: Extract only the last word from a string
vbscript, using arrays
Code: Select all
myString = "one two three four five"
myArray = Split(myString)
WScript.Echo myArray( UBound(myArray) )