Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How to remove multispaces from text?

Featured Replies

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

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.

  • Author

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

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.

  • 3 weeks later...

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.