Page 1 of 1

Search user input for character

Posted: 12 Feb 2016 07:21
by batchcc
I have the following code to get user input and I want to search their input for a character and set anything before that character as %before% the character as %char% and after that character as %after% does anyone have any idea on how to do this? Thanks everyone!

Code: Select all

@echo off
Cls
Set /p input="enter a word "
::Search for character
Set before=
Set char=
Set after=

Re: Search user input for character

Posted: 12 Feb 2016 07:40
by Squashman
You really have not provided enough detail or any examples of what the input and output would look like. What happens when their are multiple matches?

Re: Search user input for character

Posted: 12 Feb 2016 08:37
by batchcc
If the user input has more than one of a character let's say "e" always use the first occurrence of the letter for example "there"

Code: Select all

Set before= th
Set char=e
Set after=re

The input will always be one word but may contain -^/+* and any number or lowercase letter

Re: Search user input for character

Posted: 12 Feb 2016 08:59
by Squashman
I use this function to find the zero based offset location of a string within another string.

Code: Select all

REM Subroutine to locate a string within another string and return the 0 based offset (-1 indicates not found)
:Instr [target-string] [search-string] [loc-var]
  set "%~3=-1" & set "_str1=%~1"
  call :StrLen "%~1" _len1 & call :StrLen "%~2" _len2 & set /A "_chk=_len1-_len2"
  for %%A in (!_len2!) do (for /L %%B in (0,1,!_chk!) do (if /I "%~2" == "!_str1:~%%B,%%A!" (set "%~3=%%B" & exit /b)))
  exit /b
 
 REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & set "_len=0"
  for /L %%A in (12,-1,0) do (set /a "_len|=1<<%%A" & for %%B in (!_len!) do if "!_str:~%%B,1!"=="" set /a "_len&=~1<<%%A")
  set /a %~2=%_len%
  exit /b


Use the CALL command to use it.

Code: Select all

call :InStr "there" "e" Loc


The CALL to the function will return the offset of the character you are looking for in the variable called Loc.

Once you know the position of the character you can then use the SET command to substring the input.

Re: Search user input for character

Posted: 12 Feb 2016 10:25
by batchcc
Every time I try this code even when the input has no "e" the variable %loc% echos -1

Re: Search user input for character

Posted: 12 Feb 2016 10:34
by Squashman
batchcc wrote:Every time I try this code even when the input has no "e" the variable %loc% echos -1

If it does not find the character it defaults to -1.

Please show us the code you are using. I am not going to guess at how you are trying to use the code.

Re: Search user input for character

Posted: 12 Feb 2016 10:37
by batchcc
loc.bat

Code: Select all

@echo off
cls
call :InStr "there" "e" Loc
echo %loc%
pause

REM Subroutine to locate a string within another string and return the 0 based offset (-1 indicates not found)
:Instr [target-string] [search-string] [loc-var]
  set "%~3=-1" & set "_str1=%~1"
  call :StrLen "%~1" _len1 & call :StrLen "%~2" _len2 & set /A "_chk=_len1-_len2"
  for %%A in (!_len2!) do (for /L %%B in (0,1,!_chk!) do (if /I "%~2" == "!_str1:~%%B,%%A!" (set "%~3=%%B" & exit /b)))
  exit /b
 
 REM Subroutine to return the length of a string variable
:StrLen [string] [len-var]
  set "_str=A%~1" & set "_len=0"
  for /L %%A in (12,-1,0) do (set /a "_len|=1<<%%A" & for %%B in (!_len!) do if "!_str:~%%B,1!"=="" set /a "_len&=~1<<%%A")
  set /a %~2=%_len%
  exit /b

Re: Search user input for character

Posted: 12 Feb 2016 10:38
by Squashman
Did you notice all the variables using exclamation marks for expansion? That should tell you something.

You might want to put a GOTO :EOF before the functions in your batch file as well.

Re: Search user input for character

Posted: 12 Feb 2016 11:16
by Aacini

Code: Select all

@echo off
Cls
Set /p input="enter a word "

::Search for character "e"
Set "char=e"

for /F "tokens=1* delims=%char%" %%a in ("%input%") do (
   Set "before=%%a"
   Set "after=%%b"
)

echo Before: "%before%"
echo Char:   "%char%"
echo After:  "%after%"

Re: Search user input for character

Posted: 12 Feb 2016 11:47
by Squashman
Antonio +1!
I way over thought that.
K.I.S.S.

Re: Search user input for character

Posted: 13 Feb 2016 13:50
by batchcc
Thanks Aacini and Squashman, Aacini's code worked but just for my reference in the future if I wanted to ignore the first "e" and use the second occurrence of the letter "e" is there an easy way to do this as well? Thanks

Re: Search user input for character

Posted: 13 Feb 2016 19:39
by Squashman
batchcc wrote:Thanks Aacini and Squashman, Aacini's code worked but just for my reference in the future if I wanted to ignore the first "e" and use the second occurrence of the letter "e" is there an easy way to do this as well? Thanks

It is as simple as using two tokens and using a, b, and c as your variables.

Code: Select all

@echo off
Cls
Set "input=therefore"

::Search for character "e"
Set "char=e"

for /F "tokens=1,2* delims=%char%" %%a in ("%input%") do (
   Set "part1=%%a"
   Set "part2=%%b"
   set "part3=%%c
)

echo Before: "%part1%%char%%part2%"
echo Char:   "%char%"
echo After:  "%part3%"

pause

output

Code: Select all

Before: "ther"
Char:   "e"
After:  "fore"
Press any key to continue . . .