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
2010-07-02 08:45:30
Didn't see this before I answered :)
Amethi
2010-07-02 08:46:25
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
2010-07-02 08:45:51
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
2010-09-06 08:36:43
+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
2010-07-02 08:46:10
+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
2010-07-02 09:13:38
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
2010-09-06 08:35:57