Exclude words?

Discussion in 'Mac Programming' started by themacster298, Apr 21, 2012.

  1. macrumors member

    Joined:
    Feb 20, 2011
    #1
    New to programming here and need some help.

    How would I go about excluding words when someone enters one in a text field?

    For example, if someone types "My favorite number is 32". How would I exclude everything other than the number to store it in a variable with a push of a button?
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    There are lots of ways to do this ranging from the naive to natural language processing. If you are new to programming I would recommend changing the design of your interface such that "My favorite number is:" is a label, and you have a field that only accepts a number.

    -Lee
     
  3. thread starter macrumors member

    Joined:
    Feb 20, 2011
    #3
    Thanks for the tips, but I am actually interested in doing it the other way. Thanks for the help, though :)
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    OK. Is that EXACTLY the phrase they'll enter? Do you just want the LAST word at the end of the sentence? Any number that appears? The fifth word that is entered?

    -Lee
     
  5. chown33, Apr 21, 2012
    Last edited: Apr 21, 2012

    macrumors 603

    Joined:
    Aug 9, 2009
    #5
    The general documentation you should read is the "String Programming Guide". It has sections on searching and scanning strings, as well as a lot of other stuff.

    That guide should also be in the builtin Xcode documentation (use Xcode's Help menu to open the doc window, then search for String Programming Guide).


    To solve the problem, Break It Down.

    Step 1: How do you distinguish the number part of the text from the non-number part?

    Maybe look at the NSCharacterSet class, and see what it has for identifying decimal digits. Then you'd be able to classify individual characters in the text as "part of a number" and "not part of a number".


    Step 2: How do you access individual characters in the text, so they can be classified as "part of a number" and "not part of a number"?

    See the NSString class, and look for methods that return a character at a specific index, or characters in a range.

    Also look at NSString methods that let you scan and search an NSString for characters in an NSCharacterSet, and which returns the ranges of those characters.


    Step 3: Having access to the ranges or individual characters of the text, and being able to distinguish number-parts from non-number-parts, you now have sufficient information to extract the parts that are the number-part and the non-number-part. Put each part into a separate NSString. Done.

    Or see the NSScanner class. You should already be familiar with NSString and NSCharacterSet, or it won't make much sense.


    The overall strategy is basic problem solving. Break it down, describe it in plain language, repeat. Keep applying that (breaking it down, describing it plainly), until you have clearly described problems that are easy enough to solve without further breaking them down.

    Every program is an explanation. It explains to the computer exactly how to solve the problem. If you can't explain things, or don't understand what you're solving, or can't break things down, you won't be able to write programs.
     
  6. thread starter macrumors member

    Joined:
    Feb 20, 2011
    #6
    In a way, yes. I am creating a trading tool for my friend for a game called "team fortress 2". This was the instructions he gave me:

    As you can see, I only want to grab the "STEAM_0:0:1234567" part, but I have no way of doing this.

    (Also, this is not a phisher, hacker, it is a TRADING tool much like this one)
     
  7. macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #7
    Am I understanding you correctly? You want to make a tool to single out the least experienced players, and list what can be stolen from them?
    Wow, just … wow.

    And here you are, expecting people to do the work for you because you are inexperienced and don't know so much about programming?
    How ironic.
     
  8. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    I can't comment on what this is being used for, but if the format you posted is exact it seems easy enough to tokenize on spaces and grab the third token. I'm guessing there's a fixed format for Steam IDs, but Valve might not publish it. If you did know this you could write something more flexible that searched for tgus format.

    How easy any of this is depends on the language and possibly libraries you're using. Which are you using? It doesn't matter that much, but if this is for-pay you might mention it, as that's a bit sticky if someone wrote a lot of this for you.

    -Lee
     
  9. thread starter macrumors member

    Joined:
    Feb 20, 2011
    #9
    No, I am not stealing anything from anyone. Programs like this is to find useful items that you want for useful items other people want. I thought this would be a fun and challenging thing to do as a first project and just happened to run into a small problem. Sorry for this misconception.

    EDIT: Finally figured it out. Thanks for the help everyone!
     
  10. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #10
    When I glanced at this the other day I thought the easiest solution was a regex. Im surprised no one suggested it.
     
  11. macrumors 603

    Joined:
    Aug 9, 2009
    #11
    Maybe because no one knew what language he was writing in. And still, no one does.

    Or maybe because he wrote: "New to programming here ...", and he wouldn't know a regex from a ... well, anything. So now you have two problems.
     
  12. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #12
    That's part of the reason I didn't reply. No language was mentioned.

    And “new to programming” doesn't mean you couldn't work out a simple regex if it was explained.
     

Share This Page