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

Hawkeye75

macrumors member
Original poster
Mar 29, 2010
32
0
Hi folks,

I'm completely new to Iphone programming so please bear with me.

I need to make some somewhat complex checks on text entered into a UITextField. The problem is I want to be able to grab the word and give the user the option to strip any spaces, numbers or special characters from it. First I obviously need to checked if any such characters exists. My intial thoughts was to grab the word and seperate the letters into an array so that they can be check individually.

I come from SQL programming where string manipulation is much simpler so I'm still trying to wrap my head around NSString methods and which combinations will help me achieve my goal. Maybe I need to consider looking at the NSScanner class.

Any thoughts or suggestions would be great.

Thanks,
 
My apologies on the vagueness of my previous post. I'm trying to come up with a method that I can call to take a SINGLE word and store every letter in an array that it will return.

The problem is in the execution and my lack of experience with the NSString class. I had first thought of using the string length to loop through the word using 'characterAtIndex:'. However the fact that it returns a unichar type makes it difficult to work with.

Once I have the word in an array as NSString data then performing checks on individual characters should be much simpler.
 
All strings in Cocoa are made up of unicode characters. Hence the Unichar return type of that method. Assuming otherwise is normally a very bad idea. If you absolutely must get the data as char then use cStringUsingEncoding: supplying an appropriate encoding but I would stress that normally this means you are approaching the problem the wrong way.
 
All strings in Cocoa are made up of unicode characters. Hence the Unichar return type of that method. Assuming otherwise is normally a very bad idea. If you absolutely must get the data as char then use cStringUsingEncoding: supplying an appropriate encoding but I would stress that normally this means you are approaching the problem the wrong way.

Do you mean it's a bad idea to use characterAtIndex or to convert the resulting char to an NSString? Personally I'd rather user a method similar to componentsSeparatedByString: to just parse the letters from a single word and put them in an array. However since I can't specify a separator I need to come up with a different solution.

The end result is I want to be able to strip out "characters" from a word but only of a specified type.
i.e. "12Hello@" is entered
then you can choose to remove numeric characters only -> "Hello@"
or remove special characters only -> "12Hello"
or both -> "Hello"
 
Have you considered somehow using NSString's stringByReplacingOccurrencesOfString:withString: and passing an empty string (i.e. @"") to withString: ?
 
Have you considered somehow using NSString's stringByReplacingOccurrencesOfString:withString: and passing an empty string (i.e. @"") to withString: ?

That may work for removing characters but they still need to be evalutated first. In my example "12Hello@" I need to know WHICH character needs to be removed or replaced in your example. Since I need to determine which characters are actually numbers first I'll have to perform some character by character check. Hence why I'm trying to figure out how to but the letters in an array. If I can easily segregate them then it becomes much simpler.
 
Since I need to determine which characters are actually numbers first I'll have to perform some character by character check.
Wouldn't characters that match @"0", @"1", ..., @"9" be the ones that are actually numbers?

Just to be clear, I am not proposing any kind of "one-liner" solution here. You are going to need some kind of looping and logic based on the set of characters you are trying to remove in order to "process" this string.
 
Do you mean it's a bad idea to use characterAtIndex or to convert the resulting char to an NSString? Personally I'd rather user a method similar to componentsSeparatedByString: to just parse the letters from a single word and put them in an array. However since I can't specify a separator I need to come up with a different solution.

The end result is I want to be able to strip out "characters" from a word but only of a specified type.
i.e. "12Hello@" is entered
then you can choose to remove numeric characters only -> "Hello@"
or remove special characters only -> "12Hello"
or both -> "Hello"

That's all fine but you should probably be using some sort of modern regex framework. Throwing away loads of data to downsample to char is just not a good solution. At least work with Unichars if you have to do it this way.
 
Wouldn't characters that match @"0", @"1", ..., @"9" be the ones that are actually numbers?

Just to be clear, I am not proposing any kind of "one-liner" solution here. You are going to need some kind of looping and logic based on the set of characters you are trying to remove in order to "process" this string.

That's all fine but you should probably be using some sort of modern regex framework. Throwing away loads of data to downsample to char is just not a good solution. At least work with Unichars if you have to do it this way.

dejo - You are probably right, it might be easier to just create multiple sets of arrays containing the data that I would want to strip out and loop through that way.

robbieduncan - I had to look up regex. :) Although considering how new I am at this I'm not sure using an unsupported framework would be a good idea...at least not just yet. As well I'm sorry but I'm still not understand your meaning in reference to using unichar? Just the result of using characterAtIndex: gave me so much grief. That text couldn't be sent to a Text Field or sent as an argument for most NSString class methods. Took me forever just to figure out how to convert it to something I could use.

However I do appreciate the feedback...it's just a lot to process when you are still trying to get a handle on things. :D
 
robbieduncan - I had to look up regex. :) Although considering how new I am at this I'm not sure using an unsupported framework would be a good idea...at least not just yet. As well I'm sorry but I'm still not understand your meaning in reference to using unichar? Just the result of using characterAtIndex: gave me so much grief. That text couldn't be sent to a Text Field or sent as an argument for most NSString class methods. Took me forever just to figure out how to convert it to something I could use.


Well char arrays will, potentially, be worse for that not better!
 
If you want to keep only digits, and feel a bit adventurous, you could try this:

Code:
NSString *resultString = [[originalString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
 
If you want to keep only digits, and feel a bit adventurous, you could try this:

Code:
NSString *resultString = [[originalString componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];

THANK YOU bredell! That works like a charm! :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.