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
String clean up. remove starting empty space
Re: String clean up. remove starting empty space
Here is an example of the string I get and want to clean up either side.
You will see empty space before and after the text.
Code: Select all
Sample text
Re: String clean up. remove starting empty space
- Try this:
- By default Trim/LTrim/RTrim removes leading/trailing spaces and tabs. You can specify a list of characters.
Code: Select all
vText := " text "
MsgBox, % "[" Trim(vText) "]"
MsgBox, % "[" LTrim(vText) "]"
MsgBox, % "[" RTrim(vText) "]"
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: String clean up. remove starting empty space
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
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
Re: String clean up. remove starting empty space
- 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:
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: String clean up. remove starting empty space
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.
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.
Re: String clean up. remove starting empty space
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%
vText := sampleText
vText := Trim(vText, Chr(10))
vText := LTrim(vText)
vText := RTrim(vText)
MsgBox, %vText%
Re: String clean up. remove starting empty space
- Perhaps one of these two:
- 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.
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(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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: String clean up. remove starting empty space
Thank you for the reply and explantions.
Very helplful answer.
Very helplful answer.
Re: String clean up. remove starting empty space
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:
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:
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%
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
Re: String clean up. remove starting empty space
- Btw I intended for only one of these lines to be used, I would suggest the 2nd one:
- 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
vText2 := Trim(vText2, "`n `t") ;trim LFs and spaces/tabs
vText2 := Trim(vText2, "`r`n `t") ;trim CRs/LFs and spaces/tabs
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: String clean up. remove starting empty space
Once again thanks! I really appreciate the answer and feedback.
Who is online
Users browsing this forum: Billykid, dinodragon, mcd and 149 guests