Author Topic: INSTR and LEN Question  (Read 3265 times)

0 Members and 1 Guest are viewing this topic.

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
INSTR and LEN Question
« 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

« Last Edit: March 11, 2020, 04:51:41 am by odin »

Marked as best answer by odin on March 11, 2020, 12:52:21 am

Offline visionmercer

  • Newbie
  • Posts: 8
    • View Profile
Re: INSTR and LEN Question
« Reply #1 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.
« Last Edit: March 11, 2020, 04:51:56 am by odin »

Offline MLambert

  • Forum Regular
  • Posts: 115
    • View Profile
Re: INSTR and LEN Question
« Reply #2 on: March 11, 2020, 07:46:59 am »
thanks .. what about the INSTR ??

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
Re: INSTR and LEN Question
« Reply #3 on: March 11, 2020, 11:33:58 am »
Probably looking at invisible spaces. Use LTRIM$(RTRIM$()) right after conversion to a string.
You're not done when it works, you're done when it's right.

Offline _vince

  • Seasoned Forum Regular
  • Posts: 422
    • View Profile
Re: INSTR and LEN Question
« Reply #4 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

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: INSTR and LEN Question
« Reply #5 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.