Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Filtering strings

edited August 2013 in Questions Posts: 1,976

Say I have a box of text. I have what the previous text was, before I typed something, and I have what the text box holds after I typed it. How can I tell the difference? What I mean by that is, how can I filter out the old text from the current text so I can tell what I typed?

And, the real scenario isn't with a text box, I just used that as an example, so don't tell me about callbacks.

Comments

  • dave1707dave1707 Mod
    Posts: 7,673

    Is this what you want or is the new text scattered among the old text.


    function setup()     oldText="this is the old text."     newText=oldText.."This is the new text that was typed"     print(oldText)     print(newText)          s,e=string.find(newText,oldText,1)     print(string.sub(newText,e+1)) end
  • Posts: 1,976

    @dave1707 That's close to what I needed, but I forgot to mention that the new text is before the old text, and when I swap around the positions of the new text and old text it returns an empty string.

  • edited August 2013 Posts: 1,976

    @dave1707 Oh, haha. Swapped it a little bit more in different places and this works:

    print(string.sub(newText,1,s-1))
    

    Thanks!

  • edited August 2013 Posts: 157

    if I understand correctly, you want to overlay two strings, like this:
    "foo", "foosball"
    and you want to konw tha the difference between the two is "sball", correct?

    While I can't give you the specific Lua syntax (I'm not that conversant with Lua yet), the general practice would be to loop through both strings, one character at a time, until I find a mismatch. Then report the difference back to my caller. (Ah, I see a "find" example was used above. That's another way to do it.)

    Here's a rough example. Assume string1 is your "before" value and string2 is your "after" value.

    pos=1
    while(pos < string1.len && pos < string2.len 
        && string1.sub(pos,pos) == string2.substring(pos,pos)) do
    
        pos = pos + 1
    end
    

    at the end of that little loop, you'll have the position of the first differing character... or the end of one of the two strings.

    Since you can't be more specific with your question, I can't really give you a more specific suggestion than that. =)

  • edited August 2013 Posts: 1,976

    Actually, I can't seem to get either one of these working where I need it to. I've figured out this:

    string.sub(newText, 1, string.len(newText) - string.len(oldText))
    

    I'm 99.99% sure this should work, but it just seems to be random every time, with string.len(newText) - string.len(oldText) ranging from 4 to -35.

    Any more help?

  • dave1707dave1707 Mod
    Posts: 7,673

    I put the new text in front of the old text this time.


    function setup()     oldText="this is the old text."     newText="This is the new text that was typed."..oldText     print(oldText)     print(newText)          s,e=string.find(newText,oldText,1)     print(string.sub(newText,1,s-1)) end
  • Posts: 1,976

    I already figured that out, but what about my most recent solution? Any idea why it's random?

  • dave1707dave1707 Mod
    Posts: 7,673

    As far as I can tell, your random code should work. Can you post a simple example of when it doesn't work.

  • Posts: 1,976

    I can't get it to not work anywhere else other than what I'm working on, but I'm not quite ready to share it yet...

  • Posts: 1,976

    If I have to, I can share it with you, but only if you must see it. But I need this really badly, so if I must, you must.

  • dave1707dave1707 Mod
    Posts: 7,673

    What is the size of the (new text+old text) and the size of the (old text) when it doesn't work. The new+old should always be larger than the old. Above you were showing 4 to -35. You should never have a negative value.

  • Posts: 1,976

    I know, that's what I don't get! The text sizes alone are around 800. Together near two thousand. I'll just show you the project by PM...

Sign In or Register to comment.