Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code - How do I find a space character in a variable? 1

Status
Not open for further replies.

emaduddeen

Programmer
Mar 22, 2007
184
US
Hi Everyone,

Can you tell me what code to use to determine if any space characters are in an entry field?

As an example this would have one: TEST 123

Thanks.
Emad
 
try this:

loop i# = 1 to len(your_var)
if sub(your_var,i#,1) = ' ' then message('space').
end
 
Code:
! All of the following are equivalent.

IF INSTRING(' ', sVar, 1, 1) 
IF INSTRING(' ', sVar)
IF INSTRING('<32>', sVar)

If you need to test for multiple characters, you might be interested in the results of an old competition for the most efficient code.

As Robert pointed out, you can use SUB().
As you may or may not know, all strings have an implicit array over them. You can also use STRING SLICING as well.

Note: if you use the Array / Slice approach it is up to you to make sure that you stay inside the array. IOW, checking sVar[42] when sVar is a STRING(12) would be a no-no. Where you normally set Debug vs. Release in your project, you can set the Runtime check [ ] Array Index. This will cause your program to complain when you go out of bounds, vs. HOPING for a GPF to tip you off that you're corrupting memory.


Code:
FindSpace PROCEDURE( sVar STRING) !LONG
LookFor STRING('<32>') 
       !I like to use '<32>' vs. ' ' for clarity.
N      LONG
RetVal LONG(FALSE)
 CODE
 LOOP N = 1 TO SIZE(sVar)
   IF sVar[N] = LookFor
      RetVal = N 
      BREAK
   END
 END
 RETURN RetVal

HTH,
Mark Goldberg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top