Subtracting one string from another Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Subtracting one string from another

Post by Lem2001 » 16 Dec 2022, 12:25

I can't think of a better way to word the title but ....

If I have a variable String1 containing the text: The quick brown fox jumped over the lazy dog
and another variable String2 containing any contiguous segment of String1 e.g. : fox jumped over the

How do I remove String2 from String1 so that I am left with String3 The quick brown lazy dog

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Subtracting one string from another

Post by flyingDman » 16 Dec 2022, 12:27

Strreplace . https://www.autohotkey.com/docs/commands/StrReplace.htm

Code: Select all

var := "The quick brown fox jumped over the lazy dog"
msgbox % strreplace(var, "fox jumped over the")
Last edited by flyingDman on 16 Dec 2022, 12:30, edited 1 time in total.
14.3 & 1.3.7

Rohwedder
Posts: 7771
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Subtracting one string from another  Topic is solved

Post by Rohwedder » 16 Dec 2022, 12:29

Hallo,
try:

Code: Select all

String1 := "The quick brown fox jumped over the lazy dog"
String2 := "fox jumped over the "
String3 := StrReplace(String1, String2)
MsgBox,% "String3:`n" String3

User avatar
andymbody
Posts: 994
Joined: 02 Jul 2017, 23:47

Re: Subtracting one string from another

Post by andymbody » 16 Dec 2022, 13:29

@Lem2001

To be clear...

I hope this post does not step on any toes here, but this is in the interest of those who are less familiar with AHK code and commands, than those who provide the assistance.

The previous 2 posts do the job. But for those who are unfamiliar with StrReplace(), it may not be clear, how/why...?
What the previous 2 examples are not showing, is a 3rd parameter that has been left out of the example, as follows...

This...
String3 := StrReplace(String1, String2)

is actually this...
String3 := StrReplace(String1, String2, "" )

You are replacing the second parameter (String2) with the 3rd parameter, which in this case, is "nothing" (""). Essentially "deleting/removing" the target string2 from the source string1. The value of the third parameter defaults to "nothing", if it is not included. If you wanted to "replace" String2 with "something", then you would include the replacement "something" using the 3rd parameter.

This is simple enough to understand (once explained) but when the 3rd parameter is left out and presented to someone who doesn't know it actually exists, it makes little sense as to how/why the examples above accomplish the intended task.

Andy

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Subtracting one string from another

Post by flyingDman » 16 Dec 2022, 13:45

@andymbody Read the docs:
20221216_103958.jpg
20221216_103958.jpg (13.81 KiB) Viewed 392 times
The red square brackets denote optional parameters. In addition look at the 1st example provided in the docs: no 3rd parameter.
Your post did not step on any toes but it did not help the OP.
14.3 & 1.3.7

Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Re: Subtracting one string from another

Post by Lem2001 » 16 Dec 2022, 14:08

Thanks to everyone who replied.

I now have it working,

User avatar
andymbody
Posts: 994
Joined: 02 Jul 2017, 23:47

Re: Subtracting one string from another

Post by andymbody » 16 Dec 2022, 14:58

flyingDman wrote:
16 Dec 2022, 13:45
@andymbody Read the docs:
20221216_103958.jpg
The red square brackets denote optional parameters. In addition look at the 1st example provided in the docs: no 3rd parameter.
Your post did not step on any toes but it did not help the OP.
Yes... I am familiar with the command, but others who have not read the docs may not be aware that there are additional optional parameters available for future reference and the next time they may need those parameters. I agree that everyone should read the docs prior to posting a question, but this does not always happen (obviously). I suppose the response you sent to me would have been the most helpful in the beginning to respond to the original question. Agreed?

Post Reply

Return to “Ask for Help (v1)”