Jump to content
Tuts 4 You

How to remove multispaces from text?


LCF-AT

Recommended Posts

Posted

Hi guys,

does anyone know any function I could use to remove all unnecessary spaces from a text?

Example:

Here I have some	text	between	 spaces
	&	tabs   I want to	trim to one
spaces   between!

= Text above should change to this text below

Here I have some text between spaces
& tabs I want to trim to one
spaces between!

Something like this.Just asking before whether there is already any module XY function I could use before trying to write any own function for this.Thanks.

greetz

Posted

So want want to replace tabs and multiple spaces with just a single space?

You can use regex replace for that. Replace "[\t ]+" with " ".

This will leave trailing spaces at the beginning and end.

Remove spaces at the start by replacing "^[\t ]+" with empty text.

Remove spaces at the end by replacing "[\t ]+$" with empty text.

  • Like 1
Posted

Hi,

thanks for the info but I can not use RegEx.Don't have any good working lib code for that (MASM).Just remember years ago I tried to do something with RegEx but could not do everything because of some lib limitations.

So now I found a old asm file called "sptrim.asm" from MASM package what does it with a litte adaptation.

OPTION PROLOGUE:NONE 
OPTION EPILOGUE:NONE 

align 4

wsptrim proc src:DWORD

  ; ---------------------------------------------------------------
  ; remove any white space duplicates and substitute a single space
  ; ---------------------------------------------------------------
    mov ecx, [esp+4]
    xor eax, eax
    sub ecx, 1
    mov edx, [esp+4]

  align 4
  stlp:
    add ecx, 1
    mov al, [ecx]
    cmp al, 9
    jne @F
    mov al, 32                      ; replace tabs with spaces
  @@:
    cmp al, 32
    jne @F
    cmp BYTE PTR [ecx+1], 32        ; test for next space
    je overit
    cmp BYTE PTR [ecx+1], 9         ; test for next tab
    je overit
  @@:
    mov [edx], al
    add edx, 1
  overit:
    test al, al                     ; test for zero AFTER its written.
    jnz stlp

    mov eax, [esp+4]

    ret 4

wsptrim endp

OPTION PROLOGUE:PrologueDef 
OPTION EPILOGUE:EpilogueDef 

greetz

  • Like 1
Posted

Crude and simple code in PureBasic...

String.s = "Here I have some	text	between	 spaces" + Chr(13) + "	&	tabs  I want to	trim to one"  + Chr(13) + "spaces   between!"

String.s = ReplaceString(String.s, "	", " ")
String.s = ReplaceString(String.s, "  ", " ")

For a = 1 To Len(String.s)
  
  b = FindString(String.s, Chr(13), a)
  
  If b
    String.s = ReplaceString(String.s, " ", "", #PB_String_CaseSensitive, b, 1)
    a = b
  EndIf
  
Next a 

Debug String.s

Ted.

  • Like 1
  • 3 weeks later...
CodeExplorer
Posted

I've thought on something like this (pseudocode):
if a space found check the next char
while (next char is space) increment space_counter
now we have old position of string and size to be removed = space_counter
should be trivial from here.
For removing chars you should use memcpy(source, destination, size):
memcpy(string+old_position+space_counter, string+old_postion+space_counter, size_of_string_after(old_position+space_counter))
 

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
×
×
  • Create New...