Sinisterly
String Manipulation Demo - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: String Manipulation Demo (/Thread-String-Manipulation-Demo)

Pages: 1 2


String Manipulation Demo - ArkPhaze - 05-15-2013

Alright, I got bored. So here's an example on some simple string manipulation in batch for converting text to lowercase, and also removing special characters.

Code:
@echo off & setlocal enabledelayedexpansion ::"Go Hang a Salami, I'm a Lasagna Hog!" set text="Go Hang a Salami I'm a Lasagna Hog!" call :ToLower !text! set text="!ToLowerR!" call :RemoveSpecial !text! set text="!RemoveSpecialR!" ::"gohangasalamiimalasagnahog" echo !text! pause & goto :eof :ToLower set ToLowerR= set tmp=%1 & set tmp=!tmp:"=! set i=0 :ToLower_loop1 set indexedChar=!tmp:~%i%,1! if defined indexedChar ( set IsChar=0 for %%H in (Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz) do ( set double=%%H if "!indexedChar!"=="!double:~0,1!" ( set ToLowerR=!ToLowerR!!double:~1,1! set IsChar=1 break ) if "!indexedChar!"=="!double:~1,1!" ( set ToLowerR=!ToLowerR!!double:~1,1! set IsChar=1 break ) ) if !IsChar!==0 ( set ToLowerR=!ToLowerR!!indexedChar! ) set /a i+=1 goto :ToLower_loop1 ) goto :eof :RemoveSpecial set RemoveSpecialR= set tmp=%1 & set tmp=!tmp:"=! set i=0 :RemoveSpecial_loop1 set indexedChar=!tmp:~%i%,1! if defined indexedChar ( for %%H in (Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz) do ( set double=%%H if "!indexedChar!"=="!double:~0,1!" ( set RemoveSpecialR=!RemoveSpecialR!!double:~0,1! break ) if "!indexedChar!"=="!double:~1,1!" ( set RemoveSpecialR=!RemoveSpecialR!!double:~1,1! break ) ) set /a i+=1 goto :RemoveSpecial_loop1 ) goto :eof :IsPalindrome goto :eof

If you're wondering... "Go Hang a Salami, I'm a Lasagna Hog!" is a palindrome (based on the regular characters and excluding special characters). I was going to come up with a recursive example in batch for checking this, but I first had to come up with a way to convert text all to lowercase (for comparison), and removing spaces and other special characters.

I didn't get that far though, I lost my boredom before I attempted. I have created a function for calculating the length of a string in batch... That would need to be used, and then I would traverse the indices from start to end, slowly meeting in the middle and returning a value as a true or false representation.

And the length would be needed because batch is absolutely lame with negative index values on substrings:
Code:
set text=abc echo !text:~-5,1!

Would not return an undefined value, but "a" in this case. And same with any other starting index below -2...


String Manipulation Demo - ArkPhaze - 05-15-2013

Alright, I got bored. So here's an example on some simple string manipulation in batch for converting text to lowercase, and also removing special characters.

Code:
@echo off & setlocal enabledelayedexpansion ::"Go Hang a Salami, I'm a Lasagna Hog!" set text="Go Hang a Salami I'm a Lasagna Hog!" call :ToLower !text! set text="!ToLowerR!" call :RemoveSpecial !text! set text="!RemoveSpecialR!" ::"gohangasalamiimalasagnahog" echo !text! pause & goto :eof :ToLower set ToLowerR= set tmp=%1 & set tmp=!tmp:"=! set i=0 :ToLower_loop1 set indexedChar=!tmp:~%i%,1! if defined indexedChar ( set IsChar=0 for %%H in (Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz) do ( set double=%%H if "!indexedChar!"=="!double:~0,1!" ( set ToLowerR=!ToLowerR!!double:~1,1! set IsChar=1 break ) if "!indexedChar!"=="!double:~1,1!" ( set ToLowerR=!ToLowerR!!double:~1,1! set IsChar=1 break ) ) if !IsChar!==0 ( set ToLowerR=!ToLowerR!!indexedChar! ) set /a i+=1 goto :ToLower_loop1 ) goto :eof :RemoveSpecial set RemoveSpecialR= set tmp=%1 & set tmp=!tmp:"=! set i=0 :RemoveSpecial_loop1 set indexedChar=!tmp:~%i%,1! if defined indexedChar ( for %%H in (Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz) do ( set double=%%H if "!indexedChar!"=="!double:~0,1!" ( set RemoveSpecialR=!RemoveSpecialR!!double:~0,1! break ) if "!indexedChar!"=="!double:~1,1!" ( set RemoveSpecialR=!RemoveSpecialR!!double:~1,1! break ) ) set /a i+=1 goto :RemoveSpecial_loop1 ) goto :eof :IsPalindrome goto :eof

If you're wondering... "Go Hang a Salami, I'm a Lasagna Hog!" is a palindrome (based on the regular characters and excluding special characters). I was going to come up with a recursive example in batch for checking this, but I first had to come up with a way to convert text all to lowercase (for comparison), and removing spaces and other special characters.

I didn't get that far though, I lost my boredom before I attempted. I have created a function for calculating the length of a string in batch... That would need to be used, and then I would traverse the indices from start to end, slowly meeting in the middle and returning a value as a true or false representation.

And the length would be needed because batch is absolutely lame with negative index values on substrings:
Code:
set text=abc echo !text:~-5,1!

Would not return an undefined value, but "a" in this case. And same with any other starting index below -2...


String Manipulation Demo - ArkPhaze - 05-15-2013

Alright, I got bored. So here's an example on some simple string manipulation in batch for converting text to lowercase, and also removing special characters.

Code:
@echo off & setlocal enabledelayedexpansion ::"Go Hang a Salami, I'm a Lasagna Hog!" set text="Go Hang a Salami I'm a Lasagna Hog!" call :ToLower !text! set text="!ToLowerR!" call :RemoveSpecial !text! set text="!RemoveSpecialR!" ::"gohangasalamiimalasagnahog" echo !text! pause & goto :eof :ToLower set ToLowerR= set tmp=%1 & set tmp=!tmp:"=! set i=0 :ToLower_loop1 set indexedChar=!tmp:~%i%,1! if defined indexedChar ( set IsChar=0 for %%H in (Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz) do ( set double=%%H if "!indexedChar!"=="!double:~0,1!" ( set ToLowerR=!ToLowerR!!double:~1,1! set IsChar=1 break ) if "!indexedChar!"=="!double:~1,1!" ( set ToLowerR=!ToLowerR!!double:~1,1! set IsChar=1 break ) ) if !IsChar!==0 ( set ToLowerR=!ToLowerR!!indexedChar! ) set /a i+=1 goto :ToLower_loop1 ) goto :eof :RemoveSpecial set RemoveSpecialR= set tmp=%1 & set tmp=!tmp:"=! set i=0 :RemoveSpecial_loop1 set indexedChar=!tmp:~%i%,1! if defined indexedChar ( for %%H in (Aa Bb Cc Dd Ee Ff Gg Hh Ii Jj Kk Ll Mm Nn Oo Pp Qq Rr Ss Tt Uu Vv Ww Xx Yy Zz) do ( set double=%%H if "!indexedChar!"=="!double:~0,1!" ( set RemoveSpecialR=!RemoveSpecialR!!double:~0,1! break ) if "!indexedChar!"=="!double:~1,1!" ( set RemoveSpecialR=!RemoveSpecialR!!double:~1,1! break ) ) set /a i+=1 goto :RemoveSpecial_loop1 ) goto :eof :IsPalindrome goto :eof

If you're wondering... "Go Hang a Salami, I'm a Lasagna Hog!" is a palindrome (based on the regular characters and excluding special characters). I was going to come up with a recursive example in batch for checking this, but I first had to come up with a way to convert text all to lowercase (for comparison), and removing spaces and other special characters.

I didn't get that far though, I lost my boredom before I attempted. I have created a function for calculating the length of a string in batch... That would need to be used, and then I would traverse the indices from start to end, slowly meeting in the middle and returning a value as a true or false representation.

And the length would be needed because batch is absolutely lame with negative index values on substrings:
Code:
set text=abc echo !text:~-5,1!

Would not return an undefined value, but "a" in this case. And same with any other starting index below -2...


RE: String Manipulation Demo - noize - 05-16-2013

That's a cool snippet of code. I've always used this code to switch chars in a string: http://www.hackcommunity.com/Thread-Contest-1337-speak-translator?pid=138400#pid138400
While that same code you use to remove special chars like double quotes from variables:

Code:
set var=%var:"=%



RE: String Manipulation Demo - noize - 05-16-2013

That's a cool snippet of code. I've always used this code to switch chars in a string: http://www.hackcommunity.com/Thread-Contest-1337-speak-translator?pid=138400#pid138400
While that same code you use to remove special chars like double quotes from variables:

Code:
set var=%var:"=%



RE: String Manipulation Demo - ArkPhaze - 05-16-2013

That's pretty basic stuff, that's not a reliable way of removing special chars in a string though because then you have hundreds of chars that you would be replacing. You go for your knowns... I know that from a-z is the standard, and if you wanted 0-9 in there, you could add that as well, so anything that is not part of the standard alphabet we leave out.

Something like this:
Code:
set var=%var:"=%

Can't be avoided if you are passing an argument that has spaces in it though. It's no different than having a file or directory path typed into cmd as an argument, you need quotes around that, otherwise it's not just one argument.

Code:
@echo off call :ShowArgs 1 2 3 call :ShowArgs "1 2 3" pause & goto :eof :ShowArgs echo %1 goto :eof



RE: String Manipulation Demo - ArkPhaze - 05-16-2013

That's pretty basic stuff, that's not a reliable way of removing special chars in a string though because then you have hundreds of chars that you would be replacing. You go for your knowns... I know that from a-z is the standard, and if you wanted 0-9 in there, you could add that as well, so anything that is not part of the standard alphabet we leave out.

Something like this:
Code:
set var=%var:"=%

Can't be avoided if you are passing an argument that has spaces in it though. It's no different than having a file or directory path typed into cmd as an argument, you need quotes around that, otherwise it's not just one argument.

Code:
@echo off call :ShowArgs 1 2 3 call :ShowArgs "1 2 3" pause & goto :eof :ShowArgs echo %1 goto :eof



RE: String Manipulation Demo - noize - 05-16-2013

(05-16-2013, 09:00 PM)ArkPhaze Wrote: That's pretty basic stuff, that's not a reliable way of removing special chars in a string though because then you have hundreds of chars that you would be replacing. You go for your knowns... I know that from a-z is the standard, and if you wanted 0-9 in there, you could add that as well, so anything that is not part of the standard alphabet we leave out.

You mean that with that method (I've posted) you can't use special chars? Yeah, I know that, and that's a limitation for sure. I'll be learning something from the code you've posted.


RE: String Manipulation Demo - noize - 05-16-2013

(05-16-2013, 09:00 PM)ArkPhaze Wrote: That's pretty basic stuff, that's not a reliable way of removing special chars in a string though because then you have hundreds of chars that you would be replacing. You go for your knowns... I know that from a-z is the standard, and if you wanted 0-9 in there, you could add that as well, so anything that is not part of the standard alphabet we leave out.

You mean that with that method (I've posted) you can't use special chars? Yeah, I know that, and that's a limitation for sure. I'll be learning something from the code you've posted.


RE: String Manipulation Demo - ArkPhaze - 05-16-2013

(05-16-2013, 09:16 PM)noize Wrote:
(05-16-2013, 09:00 PM)ArkPhaze Wrote: That's pretty basic stuff, that's not a reliable way of removing special chars in a string though because then you have hundreds of chars that you would be replacing. You go for your knowns... I know that from a-z is the standard, and if you wanted 0-9 in there, you could add that as well, so anything that is not part of the standard alphabet we leave out.

You mean that with that method (I've posted) you can't use special chars? Yeah, I know that, and that's a limitation for sure. I'll be learning something from the code you've posted.

Not exactly, but it's part of it. What I was saying is that if you tried removing all special chars individually, you would have to assume that there is no special chars that exist in that string that you have not considered, so you wouldn't do that. You would check for only what you know you want to exist in the string, not what you want to remove.

Imagine how many of these there are out there: !#@.,[\... etc...

In comparison to the 26 letters of the alphabet. That was my point.