how will we can split a string ex: str='samuelsamuel123'

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rambetha
Posts: 2
Joined: 08 Sep 2017 06:13

how will we can split a string ex: str='samuelsamuel123'

#1 Post by rambetha » 11 Sep 2017 22:17

how will we can split a string ex: str='samuelsamuel123'
the question is, I want unique laters from this str and and each later is how many times printed
ans: unique: samuel123
s 2 times
a 2 times
.
.
.
like this

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: how will we can split a string ex: str='samuelsamuel123'

#2 Post by penpen » 12 Sep 2017 09:11

Depending on the characters you want to use you could do something like that:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /f "delims==" %%a in ('set "character["') do set "%%a="
set "str=samuelsamuel123"

for /f %%a in ('cmd /d /u /c"echo(%str%" ^| find /v ""') do @set /a "character[%%a]+=1"
set "character["
goto :eof


penpen

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: how will we can split a string ex: str='samuelsamuel123'

#3 Post by Aacini » 12 Sep 2017 09:13

Code: Select all

@echo off
setlocal EnableDelayedExpansion

set "str=samuelsamuel123"

set "unique="
for /F "delims=" %%a in ('cmd /U /C set /P "=%str%" ^<nul ^| find /V ""') do (
   if not defined letter[%%~a] set "unique=!unique!%%a"
   set /A "letter[%%~a]+=1"
)

echo Unique: %unique%
set letter[

Output:

Code: Select all

Unique: samuel123
letter[1]=1
letter[2]=1
letter[3]=1
letter[a]=2
letter[e]=2
letter[l]=2
letter[m]=2
letter[s]=2
letter[u]=2

Antonio

Post Reply