Trying to manipulate the contents of a txtField in the textFieldDidEndEditing. What we want to do is rotate the entire contents left and then append the current character at the end. This is to allow a user to enter a money value without a decimal point.
This is what we have so far but I'm not sure this is the best way to approach this. Nor is this code working as expected.
Any help appreciated.
//easier input for total
if (textField == txtGrandTotal)
{
//copy contents of total to string2
NSString *string1 = txtGrandTotal.text;
NSMutableString *string2;
//now string2 contains the buffer to manipulate
string2 = [NSMutableString stringWithString: string1];
//so, copy it to strMoney2
strMoney2 = string2;
int charIndex;
//for all character in strMoney2, move them rotated left to strMoney1
for (charIndex = 0; charIndex < [strMoney2 length]; charIndex++)
{
NSString *strChar = [strMoney2 substringWithRange: NSMakeRange(charIndex, 1)];
[strMoney1 insertString:strChar atIndex:charIndex+1];
}
//now append the current character to strMoney1
NSString *strCurrentChar = [string1 substringWithRange: NSMakeRange([string1 length], 1)];
[strMoney1 appendString:strCurrentChar];
//move manipulated string back to txtGrandTotal
txtGrandTotal.text = strMoney1;
}
Out of a zillion ways to approach this, this is quite short:
NSString *res = [NSString stringWithFormat:@"%@%@",
[input substringFromIndex:1],
[input substringToIndex:1]
];
or shorter:
NSString *res = [[input substringFromIndex:1]
stringByAppendingString:[input substringToIndex:1]];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.