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
Subtracting one string from another Topic is solved
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Subtracting one string from another
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
Re: Subtracting one string from another Topic is solved
Hallo,
try:
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
Re: Subtracting one string from another
@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
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
- flyingDman
- Posts: 2848
- Joined: 29 Sep 2013, 19:01
Re: Subtracting one string from another
@andymbody Read the docs:
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.
Your post did not step on any toes but it did not help the OP.
14.3 & 1.3.7
Re: Subtracting one string from another
Thanks to everyone who replied.
I now have it working,
I now have it working,
Re: Subtracting one string from another
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?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.