News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

remove special character from a string

Started by ragdog, June 17, 2007, 07:05:35 PM

Previous topic - Next topic

ragdog

hi guys

there a algo that remove all special character from a string and add spaces?

string,string,string:string string/string

regards
ragdog

MichaelW

One simple method would be:

Using \masm32\examples\exampl09\tables, create a table with the entries for each allowable character set to 1, leaving the other entries set to zero.

Create a work buffer the same size as the original string.

For each character in the original string use the character code as an index into the table, and if the indexed byte is 1 copy the character to the work buffer.

Continue until you reach the null terminator in the original string.
eschew obfuscation

ragdog

#2
thanks for your help


I do not understand this,i mean this

only with all special character


remspace proc txt:DWORD

    mov ecx, txt
    mov edx, txt

  @@:
    mov al, [ecx]
    add ecx, 1
    cmp al, ' '     ; is it a space
    je @B
    mov [edx], al
    add edx, 1
    test al, al     ; is AL zero
    jnz @B

    ret

remspace endp


regards

ragdog

ninjarider

that wouldn't take out the special characters and insert spaces in there place. the code there would only take out spaces.

MichaelW

ragdog,

My reply was in answer to your post before you edited it. This is an example that uses a table of characters:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc

    ; Build as console app

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data

      ; Table created with tables.exe from
      ; \masm32\examples\exampl09\tables

      align 4
      table \
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0
      db 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0
      db 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      db 1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
      db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

      string db "string,string,string:string string/string",0

    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    print ADDR string,13,10

    lea edx, string

  @@:
    movzx eax, BYTE PTR[edx]
    inc edx
    test eax, eax
    jz  @F
    test BYTE PTR[table+eax], 1
    jnz @B
    mov BYTE PTR[edx-1], ' '
    jmp @B
  @@:

    print ADDR string,13,10

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


string,string,string:string string/string
string string string string string string

eschew obfuscation

ragdog

hi MichaelW

I have understood which you mean thank for the example. :U :U

greets

ragdog

ragdog

hi guys

can make also in this way for remove this spezial char db '/\:*?"<>|',0 ?
and as I copy ebx the registers in  cmp al, "\"  for compare

RemoveSpezialChar proc src:DWORD,dst:DWORD,chr:DWORD

;szstring db '1/2\3:4*5?6"7<8>9|0',0
;szchar   db '/\:*?"<>|',0

    push esi
    push edi
    push ebx

     xor ecx, ecx    ; zero counter
     mov esi, src    ; source
     mov edi, dst    ; destination
     mov ebx, chr    ; spezialchar
     xor edx, edx    ; zero location
   
@@:
     mov al, [esi]   ; read byte from address in esi
     inc esi
     inc ecx         ; increment counter
     cmp al, 0       ; test for zero
     je @Out         ; exit loop on zero
     cmp al, "\"     ; test for char
     je nxt1        ; jump over if not
     mov edx, ecx    ; store counter in ecx
        mov [edi], al   ; write byte to address in edi
     inc edi
nxt1:
 
     jmp @B
   
@Out:
     add edx, dst    ; add destination address
     mov [edx], al   ; write terminator to destination

     pop ebx
     pop edi
     pop esi
ret
RemoveSpezialChar endp

regards

ragdog

ninjarider

you have the loop that goes through the szstring. now in the middle of that loop you need a loop that checks the character to the szchar.

i agree with michealw that it would be easier and the program would run faster if you used a table.

ChillyWilly

sorry to bring up an old topic but im trying to do something similar
the table method works for me although i need to remove all spaces from the string too
what would be an easy way to clean the string of spaces afterwards


basically i have a phone number like (555) 555-5555 i want to remove the (), - and space from the string
to make it 5555555555

dedndave

that is a simple case because you want numeric characters only
strip out everything that isn't a number

jj2007

Quote from: ChillyWilly on February 06, 2012, 05:34:24 AM
basically i have a phone number like (555) 555-5555 i want to remove the (), - and space from the string
to make it 5555555555

include \masm32\include\masm32rt.inc

.data
Src db "(555) 555-5555", 0
Dest db "xxxxxxxxxxxxxxx"

.code
start:
mov esi, offset Src
mov edi, offset Dest
.Repeat
lodsb
.if al>="0" && al<="9"
stosb ; store a number
.endif
.Until !al
stosb ; store the zero delimiter
MsgBox 0, offset Dest, "Hi", MB_OK
exit

end start

hutch--

I would probably go for the table method as its easier to modify once you have it going, it takes 2 memory reads, one from the data, the other from the table using the sign extended data byte as the index for the table.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

that's great if you have thousands of numbers to do in a loop
if you only need to convert a few phone numbers, it is simpler to parse like Jochen did

        mov     esi,offset szAsciiString
        mov     edi,offset szDestBuffer
        jmp short loop01

loop00: xor     al,30h
        cmp     al,9
        ja      loop01

        xor     al,30h
        stosb

loop01: lodsb
        or      al,al
        jnz     loop00

        stosb


EDIT:
of course, that doesn't work very well because a zero digit will terminate the string   :lol
shows what you can do on the first cup of joe in the am   :P

mofiied the code so it works