Hello!
If I am getting strings with 100 random symbols in them, how can I find duplicates in them?
From string "ABCDDEFGH##12345..." the script should return "DD" and "##",
from string "1223..." the script should return "22", and so on.
All strings are 100 characters in length:
"R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"
In this string "88" and "&&" are duplicates.
Thanks!
batch to search for duplicate letters in string
Moderator: DosItHelp
Re: batch to search for duplicate letters in string
Note that I had to double the percent sign in the assignment to finally have one in the string.
Steffen
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "str=R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"
setlocal EnableDelayedExpansion
set "prev=!str:~0,1!"
for /l %%i in (1 1 99) do (
set "curr=!str:~%%i,1!"
if "!prev!"=="!curr!" echo !prev!!curr!
set "prev=!curr!"
)
pause
Re: batch to search for duplicate letters in string
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "str=R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"
setlocal EnableDelayedExpansion
for /L %%i in (0,1,98) do (
if "!str:~%%i,2!" equ "!str:~%%i,1!!str:~%%i,1!" echo !str:~%%i,2!
)
Re: batch to search for duplicate letters in string
I did not see %% after running the batch codes. An extra step may be needed to catch double percents.
I added other special characters doubled. Quotes are not addressed; an odd number caused an error.
Jerry
I added other special characters doubled. Quotes are not addressed; an odd number caused an error.
Jerry
Code: Select all
@echo off
set "str=R#**6-O0W9OXRi&FQSf1&ZcBJlwhE86D%%2CZ+-L7B+^Z2khH1<<UG>>3NbDC&&Xj@TvS!DVEA1RNuWmup^^HJnp$O)d!Yh(8^cVWTbF5GxN!!"
call :strLen str len
set /A "len-=1"
setlocal EnableDelayedExpansion
if "!str:%%=!" neq "!str!" echo %%%%
for /L %%i in (0,1,%len%) do (
if "!str:~%%i,2!" equ "!str:~%%i,1!!str:~%%i,1!" echo !str:~%%i,2!
)
endlocal & set "str=" & set "len="
goto :eof
:strLen string len -- returns the length of a string
:: -- string [in] - variable name containing the string being measured for length
:: -- len [out] - variable to be used to return the string length
:$source https://www.dostips.com
( SETLOCAL ENABLEDELAYEDEXPANSION
set "s__=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
rem it also avoids trouble in case of empty string
set "len=0"
for /L %%A in (12 -1 0) dO (
set /a "len|=1<<%%A"
for %%B in (!len!)Do if "!s__:~%%B,1!"=="" set /a "len&=~1<<%%A"
)
)
( ENDLOCAL & REM RETURN VALUES
IF "%~2" NEQ "" SET /a "%~2=%len%"
)
EXIT /b
>testdbls
%%
**
<<
>>
&&
^^
!!
Re: batch to search for duplicate letters in string
In the original post is only one percent sign in the string. I doubled it in the SET statement to have one percent sign in the resulting variable. Otherwise it would have been stripped entirely. So, it's technically correct if it doesn't find any double percent signs.
Steffen
Re: batch to search for duplicate letters in string
aGerman, how does your code, typed out below, display %% for you and not for me?
c:\Temp\Dosbatch>testAG
88
&&
Press any key to continue . . .
c:\Temp\Dosbatch>type testag.bat
@echo off
setlocal DisableDelayedExpansion
set "str=R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"
setlocal EnableDelayedExpansion
set "prev=!str:~0,1!"
for /l %%i in (1 1 99) do (
set "curr=!str:~%%i,1!"
if "!prev!"=="!curr!" echo !prev!!curr!
set "prev=!curr!"
)
pause
Re: batch to search for duplicate letters in string
It did not. And that's the point Double percents in the assignment of str yield a single percent in the variable value. Why should it ever report double percent signs?how does your code, typed out below, display %% for you and not for me?
Steffen
Re: batch to search for duplicate letters in string
Understood. I thought that a line of code to display %% if found doubled in the string might be helpful.
My thinking was that literal doubled percents, like all other characters, but maybe not quotes, are a target for identification.
Jerry
My thinking was that literal doubled percents, like all other characters, but maybe not quotes, are a target for identification.
Jerry