Sign in to follow this  
Followers 0
myspacee

Stringreplace NULL

9 posts in this topic

Hello to all,

find a bad surprise opening some text files :

Posted Image

this type of file are full of ASCII codes. Is possible to remove them ?

Try

$new_chars = StringReplace($new_chars, Asc(0), "")
without luck...

Any idea ?

thank you for help,

m.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

 stringreplace works well...if you use chr() instead of asc() :blink:

$string="Test1"&chr(0)&chr(13)&"Test2"  ;string with some ascii-codes <32
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$string' & @lf & @lf & 'Return:' & @lf & $string) ;### Debug MSGBOX

$string=stringreplace($string,chr(0),"",0,1)  ;remove the nul-byte
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$string' & @lf & @lf & 'Return:' & @lf & $string) ;### Debug MSGBOX

$string=stringreplace($string,chr(13),"",0,1)  ;remove the CarriageReturn
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$string' & @lf & @lf & 'Return:' & @lf & $string) ;### Debug MSGBOX
Edited by AndyG

Share this post


Link to post
Share on other sites

ops :blink:

thank you for reply.

As you see my strings aree full of sh1t.

My goal is to clean all char that are not in this range :

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
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
ò à ù è é ì

there is anyway to do this ?

thank you again for correction,

m.

Share this post


Link to post
Share on other sites

Global $sString = 'abcC' & chr(0) & 'Ddef'
$sString = _StringReplace($sString, Asc('b'), Asc('e'), '_')
$sString = _StringReplace($sString, 0, 0, '.')

Func _StringReplace($sString, $iFirstChar, $iLastChar, $iReplChar = '', $fCaseSensitive = True)
    If $iFirstChar > 255 Or $iLastChar > 255 Or $iReplChar > 255 Then Return SetError(2)
    For $iAsc = $iFirstChar To $iLastChar
        $sString = StringReplace($sString, Chr($iAsc), $iReplChar, 0, $fCaseSensitive)
    Next
    Return $sString
EndFunc


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

$sStr = StringRegExpReplace($sStr, "[\000-\006\016-\032\034-\037]", "")

You could also try

$sStr = StringRegExpReplace($sStr, "[[:cntrl:]]", "")

Not sure about the second one.

EDIT That was based on the probablility that you wanted to keep the rest of the characters in there. If indeed you wanted ONLY the specified characters + spaces then

$sStr = StringRegExpReplace($sStr, "[^\sa-zA-Zòàùèéì]", "")
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

thank you MvGulik,

try to modify your func to keep only Ascii code from 65 to 122 :

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
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

but no luck. can you correct me ?

Func _StringReplace($sString, $iFirstChar, $iLastChar, $iReplChar = '', $fCaseSensitive = True)
    If $iFirstChar > 255 Or $iLastChar > 255 Or $iReplChar > 255 Then Return SetError(2)
        
    For $iAsc = $iFirstChar To $iLastChar
        
        If $iAsc < 65 and $iAsc > 122  Then
            $sString = StringReplace($sString, Chr($iAsc), $iReplChar, 0, $fCaseSensitive)
        endif
        
    Next
    Return $sString
EndFunc

thank you

m.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I personally would not add a alpha-character test inside the _StringReplace*() function.

And with RegExp, like GEOSoft suggested, you can cut down the amount of code.

(There are many roads that lead to Rome. I generally like to take the scenery tour. :blink: )

Global $sString

;; option 1.
$sString = 'abcC1' & Chr(0) & '2Ddef'
ConsoleWrite('[1] $sString = ' & _ReplNull($sString) & @CRLF) ;### Debug DebugOut.
$sString = _StringReplace_IgnoreAlpha($sString, 0, 255, '.')
ConsoleWrite('[2] $sString = ' & _ReplNull($sString) & @CRLF) ;### Debug DebugOut.

Func _StringReplace_IgnoreAlpha($sString, $iFirstChar, $iLastChar, $iReplChar = '', $fCaseSensitive = True)
    If $iFirstChar > 255 Or $iLastChar > 255 Or $iReplChar > 255 Then Return SetError(2)
    For $iAsc = $iFirstChar To $iLastChar
;~      If Not (($iAsc >= 65 And $iAsc <= 90) or ($iAsc >= 97 And $iAsc <= 122)) Then _
        If Not IsAlpa($iAsc) then _
                $sString = StringReplace($sString, Chr($iAsc), $iReplChar, 0, $fCaseSensitive)
    Next
    Return $sString
EndFunc
Func IsAlpa($iAsc)
    Return (($iAsc >= 65 And $iAsc <= 90) or ($iAsc >= 97 And $iAsc <= 122))
EndFunc

;; option 2. (RegExp)
$sString = 'abcC1' & Chr(0) & '2Ddef'
ConsoleWrite('[3] $sString = ' & _ReplNull($sString) & @CRLF) ;### Debug DebugOut.
$sString = _StringRegExpReplace_IgnoreAlpha($sString, '.')
ConsoleWrite('[4] $sString = ' & _ReplNull($sString) & @CRLF) ;### Debug DebugOut.

Func _StringRegExpReplace_IgnoreAlpha($sString, $iReplChar = '')
    Return StringRegExpReplace($sString, "[^[:alpha:]]", $iReplChar)
EndFunc

;; ---
Func _ReplNull($sString) ;; consolewrite workaround for strings that contain a Null character.
    Return StringReplace($sString,Chr(0),'[NUL]')
EndFunc
---

Correction only to your code.

If $iAsc < 65 and $iAsc > 122 Then ;; is always false!

should be,

If $iAsc < 65 or $iAsc > 122 Then

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

I had is using the alpha class but that won't get the other special characters he wants to keep.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

True. Kinda forgot about those additional character in message #3.


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0