2

How can I remove all single letters from a string?

Say input string is what a string

Output should be what string

I tried set string=%string: ? : % command but ? wildcard won't work here.

I need a generic command to remove all the single letters from a string.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • wild cards are not working in a string substitution. – npocmaka Aug 01 '17 at 10:41
  • 1
    @npocmaka Well except for the `*` matching (ungreedily) everything up to the rest of the search string. `Set "String=i what a mess o things x"` and `Echo %String:*s=%` will output `s o things x` –  Aug 01 '17 at 11:25

3 Answers3

2

Given that the input string does not contain one of the characters !, ^ and ", and that there are no adjacent spaces (which would be compressed to one), the following code works:

@echo off
setlocal EnableDelayedExpansion

set "string=what a string"

set "new=" & set "wrd=" & set "aug=%string% "
set "new=%aug: =" & (if defined wrd if not "!wrd:~1!"=="" set "new=!new! !wrd!") & set "wrd=%"

echo original: "%string%"
echo modified: "%new%"

endlocal

To maintain adjacent spaces, modify the script like this:

@echo off
setlocal EnableDelayedExpansion

set "string=what  a  nice  string"

set "new=" & set "wrd= " & set "aug=%string% "
set "new=%aug: =" & (if not "!wrd:~1!"=="" set "new=!new! !wrd!") & set "wrd=%"

echo original: "%string%"
echo modified: "%new%"

endlocal

Thanks to Aacini for the hint!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Better than my batch +1. Are there references on Dostips.com for this type of self expanding code? –  Aug 01 '17 at 12:47
  • 2
    The first time I got in touch with something like this was from [this answer](https://stackoverflow.com/a/32026314), which in turn was inspired by [this DosTips post](http://www.dostips.com/forum/viewtopic.php?f=3&t=6429&p=41356#p41356). Though you will not find the code of the above answer there... – aschipfl Aug 01 '17 at 13:41
  • Wow! It seems that I opened the Pandora's Box! **`^_^`** I like this modification! **`:)`** – Aacini Aug 01 '17 at 16:36
  • 1
    I totally fell in love with such weird-looking self-expanding code things, although they often make my brain hurt `:-/`; thank you for leading me to this path, @Aacini! `;-)` – aschipfl Aug 01 '17 at 17:38
  • 2
    You are welcome! **`:-)`** If you `set "wrd= "` with _one_ character, you not need the `if defined wrd` part: `set "new=%aug: =" & (if "!wrd:~1!" neq "" set "new=!new! !wrd!") & set "wrd=%"` – Aacini Aug 01 '17 at 20:21
  • That's cool, @Aacini, thanks; as a nice side effect, multiple adjacent spaces no are longer compressed to one... – aschipfl Aug 01 '17 at 22:41
1
  • You will have to iterate all letters
  • the following batch also checks string begin/end

EDIT discovered a flaw with string begin/end, they will remove inevitably a char.
If there is more than one of a char on a line, only the first will be removed, to overcome this repeat %Letters% in the for loop.

@Echo off
Setlocal EnableExtensions EnableDelayedExpansion
Set "Letters= a b c d e f g h i j k l m n o p q r s t u v w x y z "
Set "String=i what a mess o things x"
IF "%~1" neq "" Set "String=%~1"
Echo Original  String: %String%

For %%A in (%Letters%%Letters%) Do Set "String=!String: %%A = !"
:: Echo:%String:~0,2%|Findstr /Li "%Letters%" >Nul 2>&1 && Set "String=%String:~2%"
:: Echo:%String:~-2%|Findstr /Li "%Letters%" >Nul 2>&1 && Set "String=%String:~0,-2%"

Echo Processed String: %String%

Sample output:

> RemoveSingleLetters.cmd
Original  String: i what a mess o things x
Processed String: what mess things
  • I think this should be good for me. I was trying to think of shorter code using wildcard because it is just a segment of my batch script. But this should do the job for me. – Aviral Mishra Aug 01 '17 at 11:50
  • This method would be simpler if before the `for` you insert a space at begin and end, and remove they after the `for`... – Aacini Aug 02 '17 at 00:36
0

Give a try for this function :

@echo off
setlocal enabledelayedexpansion

SET TEST=Example1 : "< = > & ^^^! |"
SET TEST2=Example2 : ">>=^^^!>=^^^!<====<^^^!<=^^^!<=<=<&&<=^^^!<=<=|<&^=^=^!<||&==="
Set TEST3=Example3 : "?????? Hello ??????"

echo;%TEST%      before Replace
call :ReplaceSpecialChar TEST "_"
echo;%TEST%      after Replace

echo;

echo;%TEST2%     before Replace
call :ReplaceSpecialChar TEST2 "_"
echo;%TEST2%     after Replace

echo;
echo;%TEST3%     before Replace
call :ReplaceSpecialChar TEST3 " "
echo;%TEST3%     after Replace

pause>nul&exit
:ReplaceSpecialChar
call :ReplaceSpecialCharTmp %1 "?" %2
call :ReplaceSpecialCharTmp %1 "&" %2
call :ReplaceSpecialCharTmp %1 "<" %2
call :ReplaceSpecialCharTmp %1 ">" %2
call :ReplaceSpecialCharTmp %1 "|" %2
call :ReplaceSpecialCharTmp %1 "=" %2
call :ReplaceSpecialCharTmp %1 "^^^!" %2
goto :eof
:ReplaceSpecialCharTmp
set work=!%1!
set /a i = 0
:ReplaceLoop
if "!work:~%i%,1!"=="" (set %1=!work!&goto :eof)
if not "!work:~%i%,1!"=="%~2" (set /a i += 1&goto ReplaceLoop)
set head=!work:~0,%i%!
set /a i += 1
set work=!head!%~3!work:~%i%!
goto :ReplaceLoop
Hackoo
  • 18,337
  • 3
  • 40
  • 70