Find a character within a string - help understanding some code
Don M
Posts: 1,653
I have this string located in the DAT section:
I am trying to find the index number or position of the comma (,) using Kye's code from the Strings Object "ASCII0_STREngine_1.spin" :
Here is how I'm using it:
It returns the value of 182 which didn't seem correct so I thought I'd put the comma at the very beginning of the string to see what happens. Then it returns the value of 108. So if I subtract 108 from 182 I get 74 which is the correct position of the comma in that string.
So my question is why is it offset by that amount? Am I not understanding / using the method correctly?
I also tried bytemove to copy the string from the DAT section to a buffer in the VAR section and it too had an offset number by some other amount. If I subtract the offset it is correct. So where is this offset coming from?
If I take the comma out it returns 0.
Thanks.
Don
DAT
Text_Plain byte "cc9a67d9fb998b18"
byte "922e015a35af0780"
byte "d6945e8473dc3fab"
byte "b3c1a7d0f0b9341d"
byte "0000000011,n0200",0
I am trying to find the index number or position of the comma (,) using Kye's code from the Strings Object "ASCII0_STREngine_1.spin" :
PUB findCharacter(stringToSearch, characterToFind) '' 5 Stack Longs
'' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
'' // Searches a string of characters for the first occurence of the specified character.
'' //
'' // Returns the address of that character if found and zero if not found.
'' //
'' // StringToSearch - A pointer to the string of characters to search.
'' // CharacterToFind - The character to find in the string of characters to search.
'' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
repeat strsize(stringToSearch--)
if(byte[++stringToSearch] == characterToFind)
return stringToSearch
Here is how I'm using it:
debug.dec(strng.findCharacter(@Text_Plain, ","))
It returns the value of 182 which didn't seem correct so I thought I'd put the comma at the very beginning of the string to see what happens. Then it returns the value of 108. So if I subtract 108 from 182 I get 74 which is the correct position of the comma in that string.
So my question is why is it offset by that amount? Am I not understanding / using the method correctly?
I also tried bytemove to copy the string from the DAT section to a buffer in the VAR section and it too had an offset number by some other amount. If I subtract the offset it is correct. So where is this offset coming from?
If I take the comma out it returns 0.
Thanks.
Don
Comments
And how do you find the string's start address?
Thanks kuroneko! I learned something today.
i := @Text_Plain debug.dec(strng.findCharacter(@Text_Plain, ",") - i)
Or this way works too...
debug.dec(strng.findCharacter(@Text_Plain, ",") - @Text_Plain)
pub cinstr(p_str, c) | idx, pos '' Returns position of character c in string at p_str '' -- returns -1 if not found longfill(@idx, -1, 2) ' preset to -1 repeat strsize(p_str) ' loop through string chars idx++ if (byte[p_str][idx] == c) ' if match pos := idx ' return position quit return pos