String clean up. remove starting empty space

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

String clean up. remove starting empty space

13 Aug 2018, 19:47

Hi all,

I have a string (taken from an html document) that I am trying to clean up.

Basically when I get the string it has numerous spaces (I believe it is spaces anyway) at the start and at the end.

So what I am looking to do is clean it up so that I am just left with the contents in the middle (spaces left and right removed).

I have seen that the trim left and right is now no longer used but I cannot fathom out how to make substr achieve this.

Kind regards,

jmcall10
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: String clean up. remove starting empty space

13 Aug 2018, 19:49

Here is an example of the string I get and want to clean up either side.

Code: Select all

                                    Sample text
                               
You will see empty space before and after the text.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: String clean up. remove starting empty space

13 Aug 2018, 20:02

- Try this:

Code: Select all

vText := " text "
MsgBox, % "[" Trim(vText) "]"
MsgBox, % "[" LTrim(vText) "]"
MsgBox, % "[" RTrim(vText) "]"
- By default Trim/LTrim/RTrim removes leading/trailing spaces and tabs. You can specify a list of characters.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: String clean up. remove starting empty space

13 Aug 2018, 20:09

Hi, thank you for the reply.

I have tried the solution and it hasnt worked in this example. Perhaps they are not spaces?
Also I thought LTrim and RTrim are no longer used?

Regards,
jmcall10
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: String clean up. remove starting empty space

13 Aug 2018, 20:13

- If you'd copied and pasted your text into the post, I could have checked it.
- StringTrimLeft/StringTrimRight are deprecated. They crop a specific number of characters, but SubStr can be used instead.
- Trim/LTrim/RTrim crop a variable number of characters, based on a list of characters.
- Try:
Trim(vText, Chr(160))
- Otherwise do something like this to get character numbers:

Code: Select all

vText := "abc"
Loop, Parse, vText
	MsgBox, % Ord(A_LoopField)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: String clean up. remove starting empty space

13 Aug 2018, 20:26

Hi, Thanks again for the response. Not sure if you missed it but I have posted the string below my original post.
Ok, so I test the "debugged" the string using ORD and basically the first char number is "10" then followed by numerous "32"'s.
Then it has my string and ends with numerous "32"'s again.
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: String clean up. remove starting empty space

13 Aug 2018, 20:30

Ok this was my solution. Not sure if it could be a lot cleaner?

vText := sampleText
vText := Trim(vText, Chr(10))
vText := LTrim(vText)
vText := RTrim(vText)
MsgBox, %vText%
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: String clean up. remove starting empty space

13 Aug 2018, 21:21

- Perhaps one of these two:

Code: Select all

vText := Trim(vText, "`n `t") ;trim LFs and spaces/tabs
vText := Trim(vText, "`r`n `t") ;trim CRs/LFs and spaces/tabs
- Chr(10) is linefeed (LF), which is also `n in AutoHotkey.
- Chr(13) is carriage return (CR), which is also `r in AutoHotkey.
- CRLF is typically used in Windows to denote a line break. Sometimes LF or CR is used.
- The forum might have formatted that LF into a CRLF. Since I didn't see a lone Chr(10) when I clicked the 'quote' button to retrieve the text of your post, that might be the explanation.
- In general, trimming CRs and LFs can be used to trim blank lines from the start/end of a block of text. Also, when you do Ctrl+C in Excel, it appends a CRLF to the end of the copied text that it puts on the clipboard.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: String clean up. remove starting empty space

13 Aug 2018, 21:32

Thank you for the reply and explantions.

Very helplful answer.
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: String clean up. remove starting empty space

13 Aug 2018, 21:44

I have a follow up question which I will keep here unless told to repost as it still relates to cleaning up my string.

I have the following now:

Code: Select all

mainContents:=

Loop, 5
{

vText1 := "Sample Text"

vText2 := "    Some more Sample text    "
vText2 := Trim(vText2, "`n `t") ;trim LFs and spaces/tabs
vText2 := Trim(vText2, "`r`n `t") ;trim CRs/LFs and spaces/tabs

mainContents=%mainContents% %vText1% - %vText2%`r

Sleep, 100

}

msgbox, %mainContents%

It is adding a space to the start of all lines from the 2nd onwards.

Any idea why this is happening? I added the `r so that each would be on a new line however I do not want the space.
Here is the output:

Code: Select all

Sample Text - Some more Sample text
 Sample Text - Some more Sample text
 Sample Text - Some more Sample text
 Sample Text - Some more Sample text
 Sample Text - Some more Sample text
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: String clean up. remove starting empty space

13 Aug 2018, 21:53

- Btw I intended for only one of these lines to be used, I would suggest the 2nd one:

Code: Select all

vText2 := Trim(vText2, "`n `t") ;trim LFs and spaces/tabs
vText2 := Trim(vText2, "`r`n `t") ;trim CRs/LFs and spaces/tabs
- You need to remove the space just after %mainContents%. Btw if you use AutoTrim, Off, the first line will have a space as well.
- I would write the script like this (more forwards compatible). Also, .= is faster for appending text IIRC.
- I used `r`n for line breaks, instead of `n or `r because it plays nice with Notepad.

Code: Select all

mainContents := ""
Loop, 5
{
	vText1 := "Sample Text"
	vText2 := "    Some more Sample text    "
	vText2 := Trim(vText2, "`r`n `t") ;trim CRs/LFs and spaces/tabs
	mainContents .= vText1 " - " vText2 "`r`n"
}
Clipboard := mainContents
MsgBox, % mainContents
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
jmcall10
Posts: 31
Joined: 25 Jan 2016, 21:27

Re: String clean up. remove starting empty space

13 Aug 2018, 22:07

Once again thanks! I really appreciate the answer and feedback.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, dinodragon, mcd and 149 guests