Shop OBEX P1 Docs P2 Docs Learn Events
Has anyone developed a string parsing object ? — Parallax Forums

Has anyone developed a string parsing object ?

woodsmithwoodsmith Posts: 1
edited 2007-03-16 18:53 in Propeller 1
I don’t want to have to reinvent the wheel if someone has already written code to parse text strings.· Specifically I need to find the location of one string inside another.· Example:

String to search: “this is a string”
String to search for: “is”
·
Desired·results: the string “is” starts at character 6 in the string “this is a string”
·
If anyone has built, worked on or is working on a similar piece of code I would like to see it.
·
I am not quite sure where to start.· I was thinking first convert the string into an array of characters, however, I am not sure how to do that.

Any help would be greatly appreciated!


Background:
I have two devices, device A sends an ASCII string to device B.· I am trying to put the prop in the middle to solve a buffering problem that device B has and to remove some of the unnecessary characters from the string.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-03-14 04:53
    My strings.spin object (http://forums.parallax.com/showthread.php?p=587930) includes a find_next function that will do what you want. It carries some baggage, though, in that it relies on my heap manager object for allocating and deallocating strings dynamically.

    -Phil
  • Tom WalkerTom Walker Posts: 509
    edited 2007-03-14 14:30
    FWIW,
    Technically, "is" appears at index 3 in your sample string.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
  • PaulPaul Posts: 263
    edited 2007-03-15 18:29
    You could use LOOKDOWN to find the sequence by assigning value to each character. I haven't done this in Spin but a similar program works in Pbasic:

    (some "Spinish" pseudocode[noparse]:)[/noparse]

    repeat n from 1 to string_length··········· ' finding string length left to the student... [noparse];)[/noparse]
    · val1 := lookdown (mystring(n) :" ","i","s") ' note begining " " to skip the "is" in "this"
    · val2 := lookdown (mystring(n+1):" ","i","s")
    · val3 := lookdown (mystring(n+2):" ","i","s")

    · if val1*100 + val2*10 + val3 == 123 'need to use magnitudes cuz " is" would = "s i" (index 4) and
    ····················································· ' "is " (index3) if you just added val's. All would = 3
    ··· do_stringfound(n+1) ' use n+1 to skip the space

    Post Edited (Paul) : 3/15/2007 6:40:05 PM GMT
  • Dennis FerronDennis Ferron Posts: 480
    edited 2007-03-16 01:56
    Tom Walker said...
    FWIW,
    Technically, "is" appears at index 3 in your sample string.

    Actually it's index 2. The first index is 0. tongue.gif

    Sorry I couldn't resist.
  • Tom WalkerTom Walker Posts: 509
    edited 2007-03-16 18:53
    Dennis,
    That's what I said....hey...where did that 3 come from??!!! ;^)

    ...I could have said it depends on the language...I've worked in various "business BASICs" that allow you to decide whether indexes start at 0 or 1...but I just goofed...sleepy after lunch, I guess...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Truly Understand the Fundamentals and the Path will be so much easier...
Sign In or Register to comment.