Convert NSString to Int - iphone

I try to convert NSString to int,Result dpPoint:0, I want dpPoint:2
dpPointStr = [NSString stringWithFormat:#"%#",[verifyRow valueForKey:#"default_point"]];
NSLog(#"dpPointStr:%#",dpPointStr); //Result dpPointStr:2
int dpPoint = [dpPointStr intValue];
NSLog(#"dpPoint:%i",dpPoint); //Result dpPoint:0

In your case, if value is in the beginning/end of the string you can try this:
int val = [[dpPointStr stringByTrimmingCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] intValue];

int str_len = [yourStr length]; char tmp[str_len];
[NSString getCString:tmp maxLength:str_len encoding:UTF8Encoding];
int dpPoint = atoi(tmp);
Don't forget to include this: #include
I think this should work just fine.
Also, I think you can fix this by taking the intValue from the:
[verifyRow valueForKey:#"default_point"];
To be consistent with Apple use NSInteger instead, or even better NSNumber for the valueForKey statement and then NSInteger.

Convert NSString to integer
NSString *sampleText = #"5";
int numValue = [sampleText intValue];
NSLog(#"Integer Value: %d", numValue);

Related

convert from string to int

i have this code:
NSString * firstdDigitTest = [numberString substringToIndex:1];
if (! [firstdDigitTest isEqualToString:#"0"])
{
numberString = [numberString substringFromIndex:1];
self.firstImageName = [namefile stringByAppendingString:firstdDigitTest];
}
self.number = [numberString integerValue];
i check if the first number is not 0 and if it's not 0 i need to insert it to the firstImageName. But when i do it (using the substringFromIndex) and i try to use integerValue it's dosent work!
without the substringFromIndex it's works! :(
NSString *str0 = #"XXX10098";
NSString *str1 = [str0 substringWithRange:NSMakeRange(4, str0.length-4)];
NSLog(#"%#", str1);
NSLog(#"%d", [str1 intValue]);
2012-10-28 10:52:07.309 iFoto[5652:907] 0098
2012-10-28 10:52:07.312 iFoto[5652:907] 98
check [numberString intValue];

Can we assign exact string value to an int value in iphone sdk

In my application I have a value like "25:30" in NSString and I want to assign this value to int value.
If I do like:
int j = [stringval intValue];
hence I got the value "25" to my int value but I want the full value.
Is it possible?
If you mean 25.30 then you need floats, use CGFloat j = [stringval floatValue];
If that is supposed to be minutes & seconds, use NSDateFormatter's dateFromString:.
You can try this
NSString *new = #"25:30";
NSArry *data = [new componentsSeparatedByString:#":"];
int first = [[data objectAtIndex:0] intValue]; \\\ 25
int second = [[data objectAtIndex:1] intValue]; \\\ 30

Unexpected result from "stringWithFormat:"

What would be the expected result from the following Objective C code?
int intValue = 1;
NSString *string = [NSString stringWithFormat:#"%+02d", intValue];
I thought the value of string would be "+01", it turns out to be "+1". Somehow "0" in format string "+01" is ignored. Change code to:
int intValue = 1;
NSString *string = [NSString stringWithFormat:#"%02d", intValue];
the value of string is now "01". It does generate the leading "0". However, if intValue is negative, as in:
int intValue = -1;
NSString *string = [NSString stringWithFormat:#"%02d", intValue];
the value of string becomes "-1", not "-01".
Did I miss anything? Or is this a known issue? What would be the recommended workaround?
Thanks in advance.
#Mark Byers is correct in his comment. Specifying '0' pads the significant digits with '0' with respect to the sign '+/-'. Instead of '0' use dot '.' which pads the significant digits with '0' irrespective of the sign.
[... stringWithFormat:#"%+.2d", 1]; // Result is #"+01"
[... stringWithFormat:#"%.2d", -1]; // Result is #"-01"
NSString *string = [NSString stringWithFormat:#"+0%d", intValue];
NSString *string = [NSString stringWithFormat:#"-0%d", intValue];

how to remove () charracter

when i convert my array by following method , it adds () charracter.
i want to remove the () how can i do it..
NSMutableArray *rowsToBeDeleted = [[NSMutableArray alloc] init];
NSString *postString =
[NSString stringWithFormat:#"%#",
rowsToBeDeleted];
int index = 0;
for (NSNumber *rowSelected in selectedArray)
{
if ([rowSelected boolValue])
{
profileName = [appDelegate.archivedItemsList objectAtIndex:index];
NSString *res = [NSString stringWithFormat:#"%d",profileName.userID];
[rowsToBeDeleted addObject:res];
}
index++;
}
UPDATE - 1
when i print my array it shows like this
(
70,
71,
72
)
Here's a brief example of deleting the given characters from a string.
NSString *someString = #"(whatever)";
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"()"];
NSMutableString *mutableCopy = [NSMutableString stringWithString:someString];
NSRange range;
for (range = [mutableCopy rangeOfCharacterFromSet:charSet];
range.location != NSNotFound;
[mutableCopy deleteCharactersInRange:range],
range = [mutableCopy rangeOfCharacterFromSet:charSet]);
All this does is get a mutable copy of the string, set up a character set with any and all characters to be stripped from the string, and find and remove each instance of those characters from the mutable copy. This might not be the cleanest way to do it (I don't know what the cleanest is) - obviously, you have the option of doing it Ziminji's way as well. Also, I abused a for loop for the hell of it. Anyway, that deletes some characters from a string and is pretty simple.
Try using NSArray’s componentsJoinedByString method to convert your array to a string:
[rowsToBeDeleted componentsJoinedByString:#", "];
The reason you are getting the parenthesis is because you are calling the toString method on the NSArray class. Therefore, it sounds like you just want to substring the resulting string. To do this, you can use a function like the following:
+ (NSString *) extractString: (NSString *)string prefix: (NSString *)prefix suffix: (NSString *)suffix {
int strLength = [string length];
int begIndex = [prefix length];
int endIndex = strLength - (begIndex + [suffix length]);
if (endIndex > 0) {
string = [string substringWithRange: NSMakeRange(begIndex, endIndex)];
}
return string;
}

Convert NSString to NSInteger?

I want to convert string data to NSInteger.
If the string is a human readable representation of a number, you can do this:
NSInteger myInt = [myString intValue];
[myString intValue] returns a cType "int"
[myString integerValue] returns a NSInteger.
In most cases I do find these simple functions by looking at apples class references, quickest way to get there is click [option] button and double-click on the class declarations (in this case NSString ).
I've found this to be the proper answer.
NSInteger myInt = [someString integerValue];
NSNumber *tempVal2=[[[NSNumberFormatter alloc] init] numberFromString:#"your text here"];
returns NULL if string or returns NSNumber
NSInteger intValue=[tempVal2 integerValue];
returns integer of NSNumber
this is safer than integerValue:
-(NSInteger)integerFromString:(NSString *)string
{
NSNumberFormatter *formatter=[[NSNumberFormatter alloc]init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *numberObj = [formatter numberFromString:string];
return [numberObj integerValue];
}
int myInt = [myString intValue];
NSLog(#"Display Int Value:%i",myInt);
NSString *string = [NSString stringWithFormat:#"%d", theinteger];