INSTR()

General FreeBASIC programming questions.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

INSTR()

Postby albert » Sep 10, 2020 21:06

If i have a string that starts with a bunch of "0"'s
How do you use INSTR() to find the first non-zero value?
jj2007
Posts: 1693
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: INSTR()

Postby jj2007 » Sep 10, 2020 21:10

You can't.
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: INSTR()

Postby albert » Sep 10, 2020 21:14

@jj2007

Can't you do something like instr( 1 , ANY ? )

How would you use the ANY function?? Is it possible..

There has to be a faster way , than looping to find the first non-zero place , or to skip over the leading zeros..

I need to do something like:

string = "0000023567"
place = instr( 1 , string , any > "0" ) ???
dodicat
Posts: 6687
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: INSTR()

Postby dodicat » Sep 10, 2020 21:27

Assuming your string could be any ascii value.

Code: Select all


dim as string s="000000000678"
dim as string nonzeros

for n as long=0 to 255
    if n<>48 then nonzeros+=chr(n)
next


print instr(s,any nonzeros)
sleep

 

If your string is only numbers or keyboard characters then nonzeros can be shrunk accordingly.
Stonemonkey
Posts: 646
Joined: Jun 09, 2005 0:08

Re: INSTR()

Postby Stonemonkey » Sep 10, 2020 21:50

Dunno if it's any use but you could get rid of leading 0's from a string of numbers with

new_string=str(val(old_string))
albert
Posts: 5916
Joined: Sep 28, 2006 2:41
Location: California, USA

Re: INSTR()

Postby albert » Sep 10, 2020 22:24

@StoneMonkey

I'm working on strings of ASCII chrs()

I need to skip over all the leading chr( 0 )'s..

The string might be a megabyte.. and as it divides by 2 , new chr( 0 )'s crop up in front..
badidea
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: INSTR()

Postby badidea » Sep 10, 2020 22:48

Code: Select all

function firstNonZeroPos(numberStr as string) as integer
   for i as integer = 0 to len(numberStr) - 1
      if numberStr[i] <> asc("0") then return i + 1
   next
   return 0
end function

print firstNonZeroPos("000123456789")
print firstNonZeroPos("123456789")
print firstNonZeroPos("000")

print instr("000123456789", any "123456789")
print instr("123456789", any "123456789")
print instr("000", any "123456789")
jj2007
Posts: 1693
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: INSTR()

Postby jj2007 » Sep 11, 2020 4:07

Code: Select all

function firstNonZero naked (numberStr as string ptr) as integer
  asm
   mov edx, [esp+4]
   dec edx
   xor eax, eax
@1:   inc eax
   cmp byte ptr [eax+edx], 48
   je @1
   cmp byte ptr [eax+edx], 0
   jne @2
   xor eax, eax
@2:   ret 4
  end asm
end function
cls
print firstNonZero(@"000123456789")
print firstNonZero(@"123456789")
print firstNonZero(@"000")
Sleep

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests