convert a char to string
my code works great until know and, if I put a double digit number into the text field (like 12) nslog returns 2 single digit numbers (like 1 and 2). Now I need to put these 2 single digit numbers into 2 strings. can somebody help me. thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]];
NSArray *components = [depositOverTotalRwy
开发者_如何学编程 componentsSeparatedByString:@"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(@"%c", extractedChar);
}
You should be able to use -stringWithFormat:
.
NSString *s = [NSString stringWithFormat:@"%c", extractedChar];
EDIT:
You can store them in an array.
NSMutableArray *digits = [NSMutableArray array];
for ( int i = 0; i < [s length]; i++ ) {
char extractedChar = [s characterAtIndex:i];
[digits addObject:[NSString stringWithFormat:@"%c", extractedChar]];
}
Try to print the value of firstThird using NSLog()
, see what it exactly hold, you code seem correct,
Use characterAtIndex
function for NSString
to extract a character at known location
- (unichar)characterAtIndex:(NSUInteger)index
Use as below
NSString *FirstDigit = [NSString stringWithFormat:@"%c", [myString characterAtIndex:0]];
NSString *SecondDigit = [NSString stringWithFormat:@"%c", [myString characterAtIndex:1]];
精彩评论