Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

themacster298

macrumors member
Original poster
Feb 20, 2011
55
0
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?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

themacster298

macrumors member
Original poster
Feb 20, 2011
55
0
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

Thanks for the tips, but I am actually interested in doing it the other way. Thanks for the help, though :)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
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.
 
Last edited:

themacster298

macrumors member
Original poster
Feb 20, 2011
55
0
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

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:

TF2Scanner:

1. Enter steam ids into text box
2. Press button to begin scanning
3. Somehow exclude the rest of the text and only leave the steam id. (Probably put it in a variable such as: tf2items.com/items/%i (%i being the steamid)
4. Show players starting with lowest hours first, with a link to their backpack.

Example of steam ID:
# 454 "R1ky" STEAM_0:0:25395679 05:20 90 0 active

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)
 

itickings

macrumors 6502a
Apr 14, 2007
947
185
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)

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

themacster298

macrumors member
Original poster
Feb 20, 2011
55
0
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.

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!
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
When I glanced at this the other day I thought the easiest solution was a regex. Im surprised no one suggested it.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
When I glanced at this the other day I thought the easiest solution was a regex. Im surprised no one suggested it.

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.
 

elppa

macrumors 68040
Nov 26, 2003
3,233
151
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.