Replacing space inside a string?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Replacing space inside a string?

Post by PuzzledGreatly » 13 Jan 2017, 18:46

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

Guest

Re: Replacing space inside a string?

Post by Guest » 13 Jan 2017, 19:00

Code: Select all

MsgBox % RegExReplace("a b  c   d", "  +", " - ")

kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: Replacing space inside a string?

Post by kon » 13 Jan 2017, 19:01

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/

User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Replacing space inside a string?

Post by JoeWinograd » 13 Jan 2017, 19:21

I don't know if it's "the most efficient way", but this simple loop will do it:

Code: Select all

Loop
{
  IfNotInString,StringVar,%A_Space%%A_Space%
    Break
  StringReplace,StringVar,StringVar,%A_Space%%A_Space%,%A_Space%
}
Then, of course, you may replace the one space left with whatever you want, such as " - " in your examples above, like this:

Code: Select all

StringReplace,StringVar,StringVar,%A_Space%,%A_Space%-%A_Space%
Regards, Joe

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Replacing space inside a string?

Post by PuzzledGreatly » 13 Jan 2017, 19:32

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:

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

It's been working but elegant it is not!

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

Re: Replacing space inside a string?

Post by jeeswg » 13 Jan 2017, 19:38

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

User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Replacing space inside a string?

Post by JoeWinograd » 13 Jan 2017, 19:43

I want to keep single spaces as they are
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).

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Replacing space inside a string?

Post by PuzzledGreatly » 13 Jan 2017, 19:58

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.

User avatar
JoeWinograd
Posts: 2214
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Replacing space inside a string?

Post by JoeWinograd » 13 Jan 2017, 21:04

Sorry, I guess I don't understand what you're trying to achieve.

mumulu
Posts: 7
Joined: 17 Mar 2023, 13:41

Re: Replacing space inside a string?

Post by mumulu » 15 Jan 2024, 17:13

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?

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Replacing space inside a string?

Post by boiler » 15 Jan 2024, 19:05

Yours is not really similar. It’s just the standard use of StrReplace():

Code: Select all

NewText := StrReplace(SourceText, " - ", " – ")

mumulu
Posts: 7
Joined: 17 Mar 2023, 13:41

Re: Replacing space inside a string?

Post by mumulu » 16 Jan 2024, 11:03

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:

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
[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

User avatar
boiler
Posts: 17404
Joined: 21 Dec 2014, 02:44

Re: Replacing space inside a string?

Post by boiler » 16 Jan 2024, 14:36

mumulu wrote: However, in your script, I guess, it works only when you type those characters.
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.

mumulu wrote: 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.
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.

Post Reply

Return to “Ask for Help (v1)”