'How can I split text when there's an uppercase character? [closed]
I am making a batch file where I want to say "welcome <username>
" but the username could be, for example, like "HiThere" and I want to put a space before the caps so it would be like "Hi There".
So, is there a way to do this?
Solution 1:[1]
It is relatively easy to use a regex to identify and add a SPACE character before upper case letters. This runs in a Windows batch file run by cmd
. If you are on a supported Windows system, PowerShell is available.
SET "THESTRING=HiThereMrNiceGuy"
FOR /F "delims=" %%A IN ('powershell -NoLogo -NoProfile -Command ^
"'%THESTRING%' -creplace '(\S)([A-Z])','$1 $2'"') DO (SET "NEWSTRING=%%~A")
ECHO NEWSTRING is set to "%NEWSTRING%"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | halfer |