Page 1 of 1

Trim with mutliple characters

Posted: Mon Apr 22, 2013 6:44 pm
by Michael Vogel

Code: Select all

Procedure.s StrTrim(String.s,Character.s=" ")

	Protected a,b
	
	a=1
	b=Len(String)

	While FindString(Character,Mid(String,b,1))
		b-1
		If b=0
			ProcedureReturn ""
		EndIf
	Wend
	
	While FindString(Character,Mid(String,a,1))
		a+1
	Wend
	
	ProcedureReturn Mid(String,a,b-a+1)

EndProcedure
Allows StrTrim(s.s,#CRLF$+#Tab$+" ")

Re: Trim with mutliple characters

Posted: Mon Apr 22, 2013 7:00 pm
by Little John
From my private string library:

Code: Select all

#WHITESPACE$ = " " + #TAB$ + #CRLF$

Procedure.s LTrimChars (source$, charlist$=#WHITESPACE$)
   ; removes from source$ all leading characters which are contained in charlist$
   Protected p, last=Len(source$)
   
   p = 1
   While p <= last And FindString(charlist$, Mid(source$,p,1)) <> 0
      p + 1
   Wend
   
   ProcedureReturn Mid(source$, p)
EndProcedure

Procedure.s RTrimChars (source$, charlist$=#WHITESPACE$)
   ; removes from source$ all trailing characters which are contained in charlist$
   Protected p
   
   p = Len(source$)
   While p >= 1 And FindString(charlist$, Mid(source$,p,1)) <> 0
      p - 1
   Wend
   
   ProcedureReturn Left(source$, p)
EndProcedure

Macro TrimChars (_source_, _charlist_=#WHITESPACE$)
   LtrimChars(RtrimChars(_source_, _charlist_), _charlist_)
EndMacro

Re: Trim with mutliple characters

Posted: Mon Apr 22, 2013 7:26 pm
by skywalk
Are you sure this is an intended trick?

Code: Select all

  Debug "~"+strTrim("11   2345 67891111 11 11 ", "11 ")+"~" ; ~2345 6789~
  Debug "~"+strTrim("11   2345 67891111 11 11 ", "1 ")+"~"   ; ~2345 6789~

Re: Trim with mutliple characters

Posted: Tue Dec 31, 2013 12:46 pm
by PB
> From my private string library:
> #WHITESPACE$ = " " + #TAB$ + #CRLF$

Don't forget this nasty little bastard: Chr(160)
It's given me so much grief in the past! :twisted:

Re: Trim with mutliple characters

Posted: Tue Dec 31, 2013 6:35 pm
by Little John
PB wrote:> From my private string library:
> #WHITESPACE$ = " " + #TAB$ + #CRLF$

Don't forget this nasty little bastard: Chr(160)
There is no universal, unambiguous definition of "whitespace".
It depends on the context which characters are considered "whitespace".
I did not forget Chr(160) -- that's deliberately not considered whitespace in my private string library (in conformance e.g. with RFC 2045 and RFC 2047).

Re: Trim with mutliple characters

Posted: Fri Jan 03, 2014 9:09 pm
by Demivec
Here's my version of a trim with multiple characters:

Code: Select all

Procedure.s MTrim(string.s, character.s = " ")
  Protected firstChar, lastChar, numChars, *ptrString.character
  
  lastChar = Len(string)
  If lastChar > 0
    
    *ptrString = @string + lastChar - 1
    
    While FindString(character, Chr(*ptrString\c))
      lastChar - 1
      If lastChar = 0
        ProcedureReturn ""
      EndIf
      *ptrString - SizeOf(character)
    Wend
    
    numChars = lastChar
    *ptrString = @string
    For firstChar = 1 To lastChar
      If Not FindString(character, Chr(*ptrString\c))
        Break ;always exits loop here
      EndIf
      numChars - 1
      *ptrString + SizeOf(character)
    Next
    
    ProcedureReturn Mid(string, firstChar, numChars)
  EndIf
  
EndProcedure
From my own tests it is faster than the other PureBasic examples when tested with these examples:

Code: Select all

);before, middle, after ;basic test
MTrim("<>abcd<>e><", "><")

;empty search string
MTrim("", "><") 

;long search string, empty character string
MTrim("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", "") 
; long search string, minimal character string
MTrim("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", "<")

;minimal search and character string
MTrim("<", ">")
;long character string
MTrim("<", "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")

Re: Trim with mutliple characters

Posted: Sat Jan 04, 2014 1:59 am
by PB
> There is no universal, unambiguous definition of "whitespace".
> It depends on the context which characters are considered "whitespace".
> I did not forget Chr(160) -- that's deliberately not considered whitespace
> in my private string library

Easy man; I was just trying to help, since your other chars in your
private string library are all non-visible characters, as Chr(160) is.
Chr(160) is really no different to Chr(32), which you included, so
I thought it a valid suggestion for you. Can't win them all.

Re: Trim with mutliple characters

Posted: Sat Jan 04, 2014 6:03 am
by Little John
PB wrote:> There is no universal, unambiguous definition of "whitespace".
> It depends on the context which characters are considered "whitespace".
> I did not forget Chr(160) -- that's deliberately not considered whitespace
> in my private string library

Easy man; I was just trying to help
Easy, man. I was just trying to help, too, by providing some additional information.

Re: Trim with mutliple characters

Posted: Sat Jan 04, 2014 6:52 am
by Shield
There are technically a lot more. For example, this is what is considered whitespace under .NET:

Code: Select all

#DOT_NET_WHITESPACE = Chr($9) + Chr($A) + Chr($B) + Chr($C) + Chr($D) + Chr($20) + Chr($85) + Chr($A0) +
                      Chr($1680) + Chr($180E) + Chr($2000) + Chr($2001) + Chr($2002) + Chr($2003) + Chr($2004) +
                      Chr($2005) + Chr($2006) + Chr($2007) + Chr($2008) + Chr($2009) + Chr($200A) + Chr($2028) +
                      Chr($2029) + Chr($202F) + Chr($205F) + Chr($3000)