QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: MLambert on March 11, 2020, 03:52:46 am

Title: INSTR and LEN Question
Post by: MLambert on March 11, 2020, 03:52:46 am
The following is a simple program


C = 20 / 3

PRINT C

C$ = STR$(C)

Z = INSTR(1, STR$(C), ".")
PRINT Z

Z = INSTR(1, C$, ".")
PRINT Z

Z = LEN(C)
PRINT Z

Z = LEN(C$)
PRINT Z

Can someone tell me why the INSTR is not showing the correct position of the decimal point
and why is the length of the fields not correct ?

Mike

Title: Re: INSTR and LEN Question
Post by: visionmercer on March 11, 2020, 04:23:27 am
STR$ returns the number with a leading space or minus
The LEN function returns the number of bytes used by a variable value and the number of characters in a STRING.
Title: Re: INSTR and LEN Question
Post by: MLambert on March 11, 2020, 07:46:59 am
thanks .. what about the INSTR ??
Title: Re: INSTR and LEN Question
Post by: STxAxTIC on March 11, 2020, 11:33:58 am
Probably looking at invisible spaces. Use LTRIM$(RTRIM$()) right after conversion to a string.
Title: Re: INSTR and LEN Question
Post by: _vince on March 11, 2020, 11:53:39 am
Probably looking at invisible spaces. Use LTRIM$(RTRIM$()) right after conversion to a string.

Which is equivalent to _TRIM$.  There's also _TRIMX$, _TRIM9$, _TRIM_butonlyonafullmoon$ which only works with Steve's version
Title: Re: INSTR and LEN Question
Post by: EricE on March 11, 2020, 12:03:58 pm
Printing double quotes, CHR$(34), around the string will show the true length of the string.

Code: QB64: [Select]
  1. C = 20 / 3
  2.  
  3. ' Frame the string with double quotes
  4. PRINT CHR$(34) + STR$(C) + CHR$(34)
  5.  
  6. C$ = STR$(C)
  7.  
  8. Z = INSTR(1, STR$(C), ".")
  9.  
  10. Z = INSTR(1, C$, ".")
  11.  
  12. Z = LEN(C)
  13.  
  14. Z = LEN(C$)
  15.