我想将字母数字字符串转换为数字。字母数字字符串包含数字数字集[0-9],大写字母集[AZ]和小写字母集[az]分别为数字[00-09],[10-35]和[36-62]。
如果字母是' y'然后号码将是' 61'或者这封信是' C'然后号码将是' 12'或者数字是' 6'那么数字就是06'
例如:
字母数字字符串:" yc69CJjVvf"
NUMBER:61380609121945315841
答案 0 :(得分:2)
创建plist,您必须手动输入数字集[0-9],大写字母集[A-Z]和小写字母集[a-z]的数据。
见下图。
并创建一个for循环,你必须传递alpha bate值。你将获得该数字。
NSString *path = [[NSBundle mainBundle] pathForResource:
@"YourPlistName" ofType:@"plist"];
NSMutableArray *arrData = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSMutableArray *arrCode=[arrData valueForKey:@"code"];
NSMutableArray *arrname=[arrData valueForKey:@"name"];
NSString *code = [[NSString alloc] init];
for (int i=0;i<[arrname count];i++)
{
if ([[arrname objectAtIndex:i] isEqualToString:StrAplahbates])//StrAplahbates is the string for which you want number value.
{
[code appendFormat:@"%@",arrCode [i]];
}
}
可能会帮助你。
答案 1 :(得分:1)
您可以使用Character-&gt; Number AND Number-&gt; Character创建NSMutableDictionary,然后您可以浏览字符串并转换字符串, 例如:
NSMutableDictionary *stringToNumbersDictionary = [[NSMutableDictionary alloc] init];
NSMutableDictionary *numberToStringDIctionary = [[NSMutableDictionary alloc] init];
for(int i = 0;i <= 9; i++) {
NSString *keyWithZero = [NSString stringWithFormat:@"0%d",i];
NSString *keyWithoutZero = [NSString stringWithFormat:@"%d",i];
// So you can look up with 06 and 6 to get the number-string
[stringToNumbersDictionary setObject:keyWithZero forKey:keyWithoutZero];
[numberToStringDIctionary setObject:keyWithoutZero forKey:keyWithZero];
}
// from ASCII A to ASCII Z
for(int i = 65;i <= 90;i++) {
NSString *key = [NSString stringWithFormat:@"%c",i];
NSString *numberKey = [NSString stringWithFormat:@"%d",i];
[stringToNumbersDictionary setObject:numberKey forKey:key];
[numberToStringDIctionary setObject:key forKey:numberKey];
}
// from ASCII a to ASCII z
for(int i = 97;i <= 122;i++) {
NSString *key = [NSString stringWithFormat:@"%c",i];
NSString *numberKey = [NSString stringWithFormat:@"%d",i];
[stringToNumbersDictionary setObject:numberKey forKey:key];
[numberToStringDIctionary setObject:key forKey:numberKey];
}
要将字符串转换为数字字符串,您可以使用
- (NSString *)numberStringWithString:(NSString *)string {
NSMutableString *result = [[NSMutableString alloc] init];
for(NSInteger index = 0;index < string.length;index++) {
unichar c = [string characterAtIndex:index];
NSString *key = [NSString stringWithFormat:@"%c",c];
[result appendString:stringToNumbersDictionary[key]];
}
return result;
}
并将其转换回来
- (NSString *)stringWithNumberString:(NSString *)numberString {
NSMutableString *result = [[NSMutableString alloc] init];
for(NSInteger index = 0;index < numberString.length;index+=2) {
NSString *key = [numberString substringWithRange:NSMakeRange(index, 2)];
[result appendString:numberToStringDIctionary[key]];
}
return result;
}
答案 2 :(得分:0)
通过char转到字符串,如果char是符号转换为数字字符串,如果char是数字而不是格式(“0%d”)并将结果累加到一个字符串而不是转换为数字。