How to check a character is alphabet or integer - iphone

In my project i have string suppose NSString* str = #"$ 120.00";
From the above string i am getting every individual character, Now i have to get all integres in the string i.e, $ is not a integer so i dont want that, "1" is a integer i want that like this . How can i do that can any one help me
Thank you

You can get a character set for digits and then use it to check your characters:
NSCharacterSet* digits = [NSCharacterSet decimalDigitCharacterSet];
if([digits characterIsMember: yourCharacter]) {
...
}

Unbeli's answer is probably the best for what it sounds like you want, which is an array of characters that happen to represent integers.
However, if your end goal is to reassemble all of the integer characters you've pulled out into a number, I'd suggest regular expressions. Cocoa doesn't have regex wrappers for replacement, but you should be able to use standard C <regex.h> code; Obj-C is a superset after all.
But that would give you 12000 in your example, as opposed to 120 which might be what you're after. In that case, I'd give [str intValue] a try.

// gcc foo.m -framework Foundation
int main()
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
NSString* str = #"$ 120.00";
const char* p = [str cStringUsingEncoding:[NSString defaultCStringEncoding]];
while( *p )
{
if( isdigit( *p ) )
{
NSLog(#"%c", *p );
}
p++;
}
[pool release];
}

Related

iOS using the index of a string to copy manual parts of a string

NSString *mainString =
#"COLOUMN1:Shaddy Van Dust COLOUMN2:245876 COLOUMN3:Info 3\nCOLOUMN1:Da Dirk COLOUMN2:45678 COLOUMN3:xcode\n";
This is an example of the NSString i want to parse.
My real string has around 31 lines.
Each information has the pre-fix "COLOUMNx" like before "Shady Van Dust".
(It's everytime COLOUMN1, COLOUMN2 & COLOUMN3, but each information after is everytime different and has a different length.)
Each line ends with '\n'.
The last token in my string is '\n'. (apart from '\0')
(With which token each line and my string generally ends doesn't matter, if this makes it easier.)
Now, i want to copy each information between COLOUMN1, COLOUMN2 & COLOUMN3 into a temp string to work with it.
That i want to repeat till the end of my string is reached.
I already tried methods like rangeOfString, but COLOUMN1, *2 & *3 exists in each line - in the same string.
What do i have to do?..
Use componentsSeparatedByString:. I'll explain a trivial way(there might be better ways of doing this):
First separate the string into an array of rows as:
NSArray* rowStrings = [mainString componentsSeparatedByString:#"\n"];
Then again use it to separate the data into columns as:
NSArray* columnStrings = [[rowStrings objectAtIndex:i] componentsSeparatedByString:#" COLOUMN"];
You'll have to remove the : again as NSString* columnData = [[columnStrings componentsSeparatedByString:#":"] objectAtIndex:1];
You can try using :
NSMutableArray *arrayOfStrings = [mainString componentsSeparatedByString:#"\n"];
Which will give you an NSStrings array like this :
0 -> COLOUMN1:Shaddy Van Dust COLOUMN2:245876 COLOUMN3:Info 3
1 -> COLOUMN1:Da Dirk COLOUMN2:45678 COLOUMN3:xcode
2 -> [...]
The array contains NSStrings, which you can access with [arrayOfStrings objectAtIndex:index];.
I'm not really sure what you can do with that, but I think you're getting closer to having the separate strings you wanted...
Hope it helps !
EDIT :
You can go further with that. Initiate a loop going through your array.
Get the string of index i :
NSString *stringToEdit = [arrayOfStrings objectAtIndex: i];
Divide your string into another array :
NSMutableArray *arrayOfLine = [stringToEdit componentsSeparatedByString:#"COLOUMN"];
You now have an new array :
[#"", #"1:Shaddy Van Dust ", #"2:245876 ", #"3:Info 3"]
Please note the empty string at the beginning, due to the fact that the string you divide starts with your separator, and the space at the end of the second and third string.
Now access this array again, and you will be able to work on your strings !
Good luck !
How about this?
NSString *mainString =
#"COLOUMN1:Shaddy Van Dust COLOUMN2:245876 COLOUMN3:Info 3\nCOLOUMN1:Da Dirk COLOUMN2:45678 COLOUMN3:xcode\n";
NSArray *strArray = [mainString componentsSeparatedByString:#"COLOUMN"];
NSMutableDictionary *stringDictionary = [[NSMutableDictionary alloc] init];
NSInteger idx = 1;
for (NSString *thisString in strArray) {
if ([thisString length] > 2) {
NSString *trimmedString = [thisString substringFromIndex:2];
[stringDictionary setValue:[trimmedString stringByReplacingOccurrencesOfString:#"\n" withString:#""]
forKey:[NSString stringWithFormat:#"String%d",idx]];
idx++;
}
}
NSLog(#"stringDictionary = %#", [stringDictionary description]);
Outputs:
2012-09-13 07:16:02.064 SO String Parsing[21203:f803] stringDictionary = {
String1 = "Shaddy Van Dust ";
String2 = "245876 ";
String3 = "Info 3";
String4 = "Da Dirk ";
String5 = "45678 ";
String6 = xcode;
}

get ascii code from string in xcode for iphone app

hey just a couple quick noob questions about writing my first ios app. Ive been searching through the questions here but they all seem to address questions more advanced than mine so im getting confused.
(1) All I want to do is turn a string into an array of integers representing the ASCII code. In other words, I want to convert:
"This is some string. It has spaces, punctuation, AND capitals."
into an array with 62 integers.
(2) How do I get back from the NSArray to a string?
(3) Also, are these expensive operations in terms of memory or computation time? It seems like it might be if we have to create a new variable at every iteration or something.
I know how to declare all the variables and im assuming I run a loop through the length of the string and at each iteration I somehow get the character and convert it into a number with some call to a built in command.
Thanks for any help you can offer or links to posts that might help!
if you want to store the ascii values in an nsarray it is going to be expensive. NSArray can only hold objects so you're going to have to create an NSNumber for each ASCII value:
unsigned len = [string length];
NSMutableArray arr = [NSMutableArray arrayWithCapacity:len];
for (unsigned i = 0; i < len; ++i) {
[arr addObject:[NSNumber numberWithUnsignedShort:[string characterAtIndex:i]]];
}
2) to go back to an NSString you'll need to use an MSMutableString and append each byte to the NSMutableString.
After saying that I'd suggest you don't use this method if you can avoid it.
A better approach would be to use #EmilioPelaez's answer. To go back from a memory buffer to an NSString is simple and inexpensive compared to iterating and concatting strings.
NSString * stringFromMemory = [[NSString alloc] initWithBytes:buffer length:len encoding: NSASCIIStringEncoding];
I ended up using the syntax I found here. Thanks for the help
How to convert ASCII value to a character in Objective-C?
NSString has a method to get the characters in an array:
NSString *string = "This is some string. It has spaces, punctuation, AND capitals.";
unichar *buffer = malloc(sizeof(unichar) * [string lenght]);
[string getCharacters:buffer range:NSMakeRange(0, [string length])];
If you check the definition of unichar, it's an unsigned short.

replace line feed char(10) in objective-c

I have some data coming in from my DB (SQL Server 2008) and it has been formatted with char(10) as the line feeds and I want to replace them on my iOS device with \n.
How would I search for the char(10)? I know I would do replaceOccurancesOfStringWith but I need to nail this character down.
Make an NSString to replace using stringWithCString:encoding:, something like
// Make the string to find
char str[2] = { 10, 0 };
NSString *toFind = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];
// Now do your replace :)
NSSTring *out = [input stringByReplacingOccurancesOfString:toFind withString:#"\\n"];

is there a way to get the character count in a string? in obj c

hii every one
is there a way to get the character count in a string in obj c?
like how does the SMS app determine how big of a bubble the text view sends and receives? thanks a lot
You can use something like this:
NSString *str = #"Hello World!";
NSUInteger len = str.length;
If it is NSString then use str.length
If it is c string then use strlen(cString)
If it is a NSString then
[string length];
Not sure if you are talking about multi-bytes characters
length
Returns the number of Unicode
characters in the receiver.
- (NSUInteger)length
Return Value
The number of Unicode characters in
the receiver. Discussion
The number returned includes the
individual characters of composed
character sequences, so you cannot use
this method to determine if a string
will be visible when printed or how
long it will appear.
NSLog(#"Length of your string is %d",[yourString length]);

Adding to a NSString

How can I create a string that can have integers added and removed to it with a simple + or -1?
You can't. What you can do is add or subtract from a number and get the string representation when you need it:
int a = 30;
a++;
a--; // etc.
NSString *numberString = [NSString stringWithFormat:#"%d", a];
If you want to append or remove characters representing integers from the end of strings, look into the NSString class.
If you're looking for operator overloading, Objective C doesn't have it.