Replacing space inside a string?
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Replacing space inside a string?
What's the most efficient way to replace an unknown number of consecutive spaces inside a string. For example:
Change: "Apples Red" into "Apples - Red"
Change: "Apples Green" into "Apples - Green"
I can only think of parsing the string character by character and testing to see if a space follows another space. I feel there's a more elegant way but I don't know what. Thanks
Change: "Apples Red" into "Apples - Red"
Change: "Apples Green" into "Apples - Green"
I can only think of parsing the string character by character and testing to see if a space follows another space. I feel there's a more elegant way but I don't know what. Thanks
Re: Replacing space inside a string?
Code: Select all
MsgBox % RegExReplace("a b c d", " +", " - ")
Re: Replacing space inside a string?
This should be trivial with RegExReplace. Match two or more consecutive spaces and replace them with whatever.
Interesting read: https://autohotkey.com/board/topic/8690 ... -with-one/
Interesting read: https://autohotkey.com/board/topic/8690 ... -with-one/
- JoeWinograd
- Posts: 2214
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Replacing space inside a string?
I don't know if it's "the most efficient way", but this simple loop will do it:
Then, of course, you may replace the one space left with whatever you want, such as " - " in your examples above, like this:
Regards, Joe
Code: Select all
Loop
{
IfNotInString,StringVar,%A_Space%%A_Space%
Break
StringReplace,StringVar,StringVar,%A_Space%%A_Space%,%A_Space%
}
Code: Select all
StringReplace,StringVar,StringVar,%A_Space%,%A_Space%-%A_Space%
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Re: Replacing space inside a string?
Thanks for the replies, I'll have a look at RegExReplace. Sorry, Joe I should have given a better example and specified that I want to keep single spaces as they are. This is what I came up with:
It's been working but elegant it is not!
Code: Select all
string := "Apples Red and Green"
ReCheck:
count = 0
loop, parse, string
{
if A_loopfield is space
count ++
else
{
if count > 1
break
else
count = 0
}
}
if count > 1
{
rep := ""
loop, %count%
rep .= A_space
stringreplace, string, string, %rep%, %A_space%-%A_space%
Goto Recheck
}
;msgbox, 4096, OK, %String%
Return String
Re: Replacing space inside a string?
if you don't use RegExReplace then perhaps:
Code: Select all
;multiple spaces to single spaces, replace single spaces
while InStr(vText, A_Space A_Space)
StringReplace, vText, vText, % A_Space A_Space, % A_Space, All
StringReplace, vText, vText, % " ", % " - ", All
;multiple spaces to double spaces, replace double spaces
while InStr(vText, A_Space A_Space A_Space)
StringReplace, vText, vText, % A_Space A_Space A_Space, % A_Space A_Space, All
StringReplace, vText, vText, % A_Space A_Space, % " - ", All
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
- JoeWinograd
- Posts: 2214
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Replacing space inside a string?
I haven't looked at your code, but mine keeps single spaces as they are — all it does is change multiple spaces into a single space (except, of course, for the line of code at the bottom, which replaces the first space with space/hyphen/space).I want to keep single spaces as they are
- PuzzledGreatly
- Posts: 1303
- Joined: 29 Sep 2013, 22:18
Re: Replacing space inside a string?
Yes, thanks, I'm accounting for the possibility of text containing more than one lot of multiple spaces. Your code is great for stripping out multiple spaces if there is no need to replace them with something else.
- JoeWinograd
- Posts: 2214
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Replacing space inside a string?
Sorry, I guess I don't understand what you're trying to achieve.
Re: Replacing space inside a string?
I have a similar problem
how can I replace a string like " - " (space hyphen space) by " – " (space n-dash space), and that for all instances in a text?
how can I replace a string like " - " (space hyphen space) by " – " (space n-dash space), and that for all instances in a text?
Re: Replacing space inside a string?
Yours is not really similar. It’s just the standard use of StrReplace():
Code: Select all
NewText := StrReplace(SourceText, " - ", " – ")
Re: Replacing space inside a string?
Hi,
thank you for your swift reply. However, in your script, I guess, it works only when you type those characters. What I want to do is: I want to replace a single hyphen, which is surrounded by a space on either side with an n-dash surrounded by a space on either side, when the text is already there. Together with this function, I want to insert hard spaces in places where they belong. Please see my script. It is triggered by Ctrl Alt Shift + Space, which then highlightes the entire text in the segment in my application, cuts it out, replaces the programmed text snippets and inserts the text in the segment again:
[Mod edit: Replaced i-tags (italic text) with [code][/code] tags. Please use them yourself when posting code!]
Note that in the second instance, there is a hard space between the period and the number (Abb.[HARD SPACE]1) as well as between the number and the unit of measurement (0[HARD SPACE]kg). (This works OK, I just don't know how to replace the hyphen by the n-dash...)
Can you please help again?
Best regards
Mumulu
thank you for your swift reply. However, in your script, I guess, it works only when you type those characters. What I want to do is: I want to replace a single hyphen, which is surrounded by a space on either side with an n-dash surrounded by a space on either side, when the text is already there. Together with this function, I want to insert hard spaces in places where they belong. Please see my script. It is triggered by Ctrl Alt Shift + Space, which then highlightes the entire text in the segment in my application, cuts it out, replaces the programmed text snippets and inserts the text in the segment again:
Code: Select all
^!+SPACE::
Send, ^a
Send, ^x
StringUpper str, Clipboard, T
head := SubStr( str, 1 , 1 )
tail := SubStr( str, 2 )
StringReplace, clipboard, clipboard, Abb. 1, Abb. 1, All
StringReplace, clipboard, clipboard, Abb. 2, Abb. 2, All
StringReplace, clipboard, clipboard, Abb. 3, Abb. 3, All
StringReplace, clipboard, clipboard, Abb. 4, Abb. 4, All
StringReplace, clipboard, clipboard, Abb. 5, Abb. 5, All
StringReplace, clipboard, clipboard, Abb. 6, Abb. 6, All
StringReplace, clipboard, clipboard, Abb. 7, Abb. 7, All
StringReplace, clipboard, clipboard, Abb. 8, Abb. 8, All
StringReplace, clipboard, clipboard, Abb. 9, Abb. 9, All
StringReplace, clipboard, clipboard, Abb. 0, Abb. 0, All
StringReplace, clipboard, clipboard, 0 kg, 0 kg, All
StringReplace, clipboard, clipboard, 1 kg, 1 kg, All
StringReplace, clipboard, clipboard, 2 kg, 2 kg, All
StringReplace, clipboard, clipboard, 3 kg, 3 kg, All
StringReplace, clipboard, clipboard, 4 kg, 4 kg, All
;etc. ... HERE SHOULD BE THE CODE FOR REPLACING "SPACE HYPHEN SPACE" BY "SPACE N-DASH SPACE".
Send ^v
RETURN
Note that in the second instance, there is a hard space between the period and the number (Abb.[HARD SPACE]1) as well as between the number and the unit of measurement (0[HARD SPACE]kg). (This works OK, I just don't know how to replace the hyphen by the n-dash...)
Can you please help again?
Best regards
Mumulu
Re: Replacing space inside a string?
No, that is not correct. You don't type any characters. It replaces the contents of the variable that's passed to it and assigns the result to the variable the return value is assigned to, as shown. In your script's case, both variables would be Clipboard.
That's exactly what it does. You must not have implemented it correctly. Show your script with your attempt at implementing it. You are using the deprecated StringReplace command for your other replacements, so you probably are not correctly implementing the function version (which is what you should be using in general) that I showed you.