find() a larger string within a smaller string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • korean_dave

    find() a larger string within a smaller string

    stringa = "hi"
    stringb = "hiyoooo"

    I'd like it to return -1 when I do:

    returnVal = stringa.find(st ringb);

    Instead, it treats stringa as "hi" and stringb as "hi".

    How do I solve this?
  • Mensanator

    #2
    Re: find() a larger string within a smaller string

    On Nov 14, 1:20 pm, korean_dave <davidrey...@gm ail.comwrote:
    stringa = "hi"
    stringb = "hiyoooo"
    >
    I'd like it to return -1 when I do:
    >
    returnVal = stringa.find(st ringb);
    >
    Instead, it treats stringa as "hi" and stringb as "hi".
    >
    How do I solve this?
    Try this:
    >>stringa = 'hi'
    >>stringb = 'hiyoo'
    >>stringa.find( stringb)
    -1

    Comment

    • John Machin

      #3
      Re: find() a larger string within a smaller string

      On Nov 15, 6:20 am, korean_dave <davidrey...@gm ail.comwrote:
      stringa = "hi"
      stringb = "hiyoooo"
      >
      I'd like it to return -1 when I do:
      >
      returnVal = stringa.find(st ringb);
      >
      Instead, it treats stringa as "hi" and stringb as "hi".
      You appear to be gravely mistaken:

      | >>stringa = "hi"
      | >>stringb = "hiyoooo"
      | >>returnVal = stringa.find(st ringb);
      | >>returnVal
      | -1
      How do I solve this?
      You need to tell us why you thought so; then, maybe, we can help.

      Comment

      • Gary Herron

        #4
        Re: find() a larger string within a smaller string

        korean_dave wrote:
        stringa = "hi"
        stringb = "hiyoooo"
        >
        I'd like it to return -1 when I do:
        >
        returnVal = stringa.find(st ringb);
        >
        Instead, it treats stringa as "hi" and stringb as "hi".
        >
        No it doesn't. stringb is "hiyoooo" and it "treats" it that way.
        (And just what do you mean by "treat"?)
        How do I solve this?
        >
        There is nothing to solve. The expression

        stringa.find(st ringb)

        asks if "hi" contains "hiyoooo", and since it does not, it returns a -1 indicating so.

        You'll have to describe what you expected and why you expected it before we will be able to see a problem that needs solving. (And then the problem will most likely be in your expectations, not in the find method.)


        Gary Herron


        Comment

        Working...