AutoHotkey Community

It is currently May 27th, 2012, 1:23 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Trim text on both sides
PostPosted: February 21st, 2009, 7:03 pm 
Offline

Joined: February 13th, 2009, 4:34 pm
Posts: 7
Hi,

I've wondered why my text comparison wont work - it was because of spaces on both or on one end of my strings which I would like to compare. i.e. " eugen beck" or " eugen beck ".
AHK's function "autotrim on" was not what I'm looking for and it wont work for my problem.

That's the reason why I wrote this little script:
Code:
/*
from a given string, all spaces on left and right side are trimmed,
both side trimmed string is given back
J.M., 21.02.2009 / 18:54
*/

fullstringtrim(text)
{
Length := StrLen(text)

; find all spaces on text's left side
LZ_left:=0
loop %length%
{
textchar:= Asc(SubStr(text,A_index,1))
if textchar = 32 ; is it a space ?
LZ_left+=1
else
break ; first good char found, exit
}

; find all spaces on text's right side
LZ_right:=0
loop %length%
{
textchar:= Asc(SubStr(text,length+1-A_index,1))
if textchar = 32 ; is it a space ?
LZ_right+=1
else
break ; first good char found, exit
}
treturn:=SubStr(text,LZ_left+1,length-LZ_left-LZ_right)
return treturn
}


Possibly somebody else can use it.
Greetings

J.M.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2009, 3:52 pm 
Perhaps you do not understand how autotrim works. Does this not do the samething as your function?
Code:
; from a given string, all spaces on left and right side are trimmed

text := "     text with leading trailing white space    "
trimmedText := fullstringtrim(text)
msgbox, Trimmed string: [[%trimmedText%]]

fullstringtrim(text) {
  trimState := A_AutoTrim ; save current autotrim state
  AutoTrim, On           ; make sure autotrim is on
  trimmedtext = %text%     ; trim the leading/trailing white space
  AutoTrim, %trimState%     ; restore autotrim state
  return trimmedtext     ; return the trimmed text
}


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2009, 4:30 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
How to remove leading spaces, trailing spaces, or both from a string?

Code:
String := "     The Quick Brown Fox     "

StringA := RegExReplace(String, "(^\s*|\s*$)")
MsgBox,0, AllTrim, % "[" StringA "]"

StringL := RegExReplace(String, "^\s*")       
MsgBox,0, LTrim, % "[" StringL "]"

StringR := RegExReplace(String, "\s*$")
MsgBox,0, RTrim, % "[" StringR "]"

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2009, 7:08 pm 
Offline

Joined: February 13th, 2009, 4:34 pm
Posts: 7
Hi,

I've now once again tried your example, and you are right!

Anonymous wrote:
Perhaps you do not understand how autotrim works. Does this not do the samething as your function?
Code:
; from a given string, all spaces on left and right side are trimmed

text := "     text with leading trailing white space    "
trimmedText := fullstringtrim(text)
msgbox, Trimmed string: [[%trimmedText%]]

fullstringtrim(text) {
  trimState := A_AutoTrim ; save current autotrim state
  AutoTrim, On           ; make sure autotrim is on
  trimmedtext = %text%     ; trim the leading/trailing white space
  AutoTrim, %trimState%     ; restore autotrim state
  return trimmedtext     ; return the trimmed text
}


thanks

J.M.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 22nd, 2009, 9:04 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
.. and I forgot

Code:
String := "     The Quick Brown Fox     "
DllCall( "shlwapi.dll\PathRemoveBlanksA", Str,String )
MsgBox, % "[" String "]"


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Bon and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group