views:

41

answers:

4

I have a string value where it contains comma. Ex:- 1,234. I want to get the value of the string where i need only 1234. Can you please help me...

+1  A: 
NSString *myString = "1,234";
NSString *resultString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];
Alex Reynolds
Didn't see this before I answered :)
Amethi
A: 

I don't know that framework/language, but if an integer converter won't work, then strip out the commas by replacing them with null from the string and then convert to an integer.

Amethi
I just took 1,234 as an example. But in some cases when u get some unwanted characters from the server and u want to parse those strings.. u cant just use a nsnumber formatter. "stringByReplacingOccurrencesOfString" method wil help you.
Pradeep Reddy Kypa
+1  A: 

If you want to strip the comma then: -

NSString *string = @"1,234";
string = [string stringByReplacingOccurrencesOfString:@"," withString:@""];

This should return you a string with just 1234 in it.

By 'getting the value' do you mean, converting this to a NSNumber object? If so use this

NSNumber *numberFromString = [NSNumber numberWithInteger:[string integerValue]];
djhworld
+1  A: 

Instead of manually stripping out the commas, it might be more elegant (and less error-prone if you support different locales) to use an NSNumberFormatter to convert the string to a number.

Ole Begemann
I just took 1,234 as an example. But in some cases when u get some unwanted characters from the server and u want to parse those strings.. u cant just use a nsnumber formatter. "stringByReplacingOccurrencesOfString" method wil help you.
Pradeep Reddy Kypa