How to check if a string contains text or is it empty?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

14 Sep 2019, 17:27

I do not understand how this can be useful in my case.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

14 Sep 2019, 17:32

I tried this but it didn’t work

Code: Select all

p::
Field = ""
Loop, parse, Field, 
If ( Field:= "" )
{    
send zzz
}
return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to check if a string contains text or is it empty?

14 Sep 2019, 18:11

Code: Select all

;either:
if (var = "")

;or:
if (var == "")

;==================================================

;note:
if (var := "")
;is equivalent to:
var := ""
if (var = "")
Perhaps for your script, you want something like this:

Code: Select all

vText := "abc,def,,ghi"
Loop Parse, vText, % ","
{
	if (A_LoopField = "")
	{
		MsgBox, % A_Index
	}
}
return
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
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: How to check if a string contains text or is it empty?

14 Sep 2019, 18:37

And I do not understand, how this could not be useful in your case.

Code: Select all

var := "abc"

F6::
if var is alpha
	msgbox, var is just text
else
	msgbox, var is or contains non-text-elements
return
Einfach nur ein toller Typ. :mrgreen:
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

14 Sep 2019, 21:38

jeeswg wrote:
14 Sep 2019, 18:11
Perhaps for your script, you want something like this:

Code: Select all

vText := "abc,def,,ghi"
Loop Parse, vText, % ","
{
	if (A_LoopField = "")
	{
		MsgBox, % A_Index
	}
}
return
MsgBox showes "3" if the line contain abc, or 123, or empty. No difference.

https://vimeo.com/user88307319/review/360050971/9fdd52105c
divanebaba wrote: And I do not understand, how this could not be useful in your case.

Code: Select all

var := "abc"

F6::
if var is alpha
	msgbox, var is just text
else
	msgbox, var is or contains non-text-elements
return
Your code same.

Sorry :oops:
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 03:25

MsgBox showes "3" if the line contain abc, or 123, or empty. No difference.
How do you read the string (line)?
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 05:43

just me wrote:
15 Sep 2019, 03:25
MsgBox showes "3" if the line contain abc, or 123, or empty. No difference.
How do you read the string (line)?
I just go to a new line.
I want the script to understand if there is text in it or is it empty. And based on the result, the next command starts, or does not start.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 07:50

jeeswg wrote:
14 Sep 2019, 18:11
Perhaps for your script, you want something like this:

Code: Select all

vText := "abc,def,,ghi"
Loop Parse, vText, % ","
{
	if (A_LoopField = "")
	{
		MsgBox, % A_Index
	}
}
return
MsgBox showes "3" if the line contain abc, or 123, or empty. No difference.
The "3" is showing you the empty field, which is at the 3rd position (A_Index).
Instead of A_Index, he could have put a MsgBox that said Empty Field Found (MsgBox, Empty Field Found!)
Jeeswg answered your question, showing you if a field is empty or not. Instead of MsgBox, you could put some other code.

Alternative Example 1

Code: Select all

vText =
(
abc, LoopField 1
def, LoopField 2

ghi, LoopField 4
)
Loop Parse, vText, `n
{
	if (A_LoopField = "")
	{
		MsgBox,,, The LoopField Number %A_Index% Is Empty!
	}
}
return
Alternative Example 2

Code: Select all

vText =
(
abc, LoopField 1
def, LoopField 2

ghi, LoopField 4
)
Loop Parse, vText, `n
{
	if (A_LoopField = "")
	{
		continue
	}
	if (A_LoopField != "")
	{
		MsgBox % A_LoopField
	}
}
return
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 09:34

reluct wrote:
14 Sep 2019, 17:32
I tried this but it didn’t work

Code: Select all

p::
Field = ""
Loop, parse, Field, 
If ( Field:= "" )
{    
send zzz
}
return
Just so you are aware of the errors in your original code, you used = and := incorrectly in both cases. You used the assignment operator := to do an if comparison. And you used = as an assignment operator with quotes, which you would only do with the expression assignment operator :=. Also, to just see if Field is blank, you don't need a loop:

Code: Select all

Field := ""
If ( Field = "" )
{    
MsgBox, zzz
}
return
You can use = to assign an empty string, but then you would not use quotes:
Field =
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 10:58

I totally don’t understand what your code does. It does not solve my problem. Maybe I'm confused with terminology. Or I’m explaining something wrong.
Imagine I'm completing sentences now, press enter. I will change to a new line where there is no text. Here the script should determine if there is text or not. I don’t know why I can’t do this with scripts that you call true.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 11:52

Are you trying to get data from a control of a GUI?

In that case, you need to use ControlGet, ControlGetText, or GuiControlGet. The usual difference between ControlGet and GuiControlGet, is if you are getting the data from a non-AutoHotkey GUI, use ControlGet or ControlGetText, or from an AutoHotkey created GUI, use GuiControlGet.

To find out the name of the control, you could use WindowSpy.ahk, usually in the directory of where you installed AutoHotkey.

https://www.autohotkey.com/docs/commands/ControlGetText.htm
ControlGetText, OutputVar , Control, WinTitle

Example

Code: Select all

p::
ControlGetText, Field, Edit1, WinTitleName
If (Field = "")
{    
	send zzz
}
return
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 12:10

@reluct As you seem to be somewhat frustrated by the responses you received, you might want to provide (much more) detailed information about your circumstance. Are you working in a word processor, in a box on a web page, in a cell of spreadsheet? can you capture the existing text using "ctrl A"? etc etc . All respondents have provided code that helps you resolve exactly what it says in the subject heading "How to check if a string contains text or is it empty?". There is obviously more to that ...
14.3 & 1.3.7
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 13:09

flyingDman wrote:
15 Sep 2019, 12:10
@reluct As you seem to be somewhat frustrated by the responses you received, you might want to provide (much more) detailed information about your circumstance. Are you working in a word processor, in a box on a web page, in a cell of spreadsheet? can you capture the existing text using "ctrl A"? etc etc . All respondents have provided code that helps you resolve exactly what it says in the subject heading "How to check if a string contains text or is it empty?". There is obviously more to that ...
Yes, I'm kind of frustrated. But this is not a problem, it is better to be frustrated with answers than when there are no answers at all. I recorded a video from the screen, I hope this helps.
https://youtu.be/87s6yrun_U8
User avatar
Chunjee
Posts: 1420
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 14:26

I've added https://biga-ahk.github.io/biga.ahk/#/?id=isundefined to biga.ahk to address cases like this. Perhaps that is of use to you. But if you are struggling with basics it may not be.

Code: Select all

A:= new biga()

A.isUndefined(neverIntializedVar)
; => true

A.isUndefined("")
; => true
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 14:32

u want to determine whether a textbox contains anything.
try @SOTE s suggestion. if the app doesnt comprise standard gui controls, ull have to find another way of grabbing the text - ctrlA ctrlC / MSAA ACC / ocr

it is only after uve grabbed it, that u can test whether its empty
Last edited by swagfag on 15 Sep 2019, 14:37, edited 1 time in total.
User avatar
Chunjee
Posts: 1420
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 14:35

I think you are trying to grab data off a website but none of the posts say so. Your video is unclear and also doesn't show any ahk code so we still don't know what you're working with.
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 22:20

Chunjee wrote:
15 Sep 2019, 14:35
I think you are trying to grab data off a website but none of the posts say so. Your video is unclear and also doesn't show any ahk code so we still don't know what you're working with.
This is Program Anki 2.0(not 2.1 !!!) https://youtu.be/87s6yrun_U8
reluct
Posts: 134
Joined: 28 Nov 2018, 05:07

Re: How to check if a string contains text or is it empty?

15 Sep 2019, 22:29

swagfag wrote:
15 Sep 2019, 14:32
u want to determine whether a textbox contains anything.
try @SOTE s suggestion. if the app doesnt comprise standard gui controls, ull have to find another way of grabbing the text - ctrlA ctrlC / MSAA ACC / ocr

it is only after uve grabbed it, that u can test whether its empty
@SOTE s suggestion not work.

But why doesn't Loop (parse a string) capture text?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 321 guests