NSString - 2 位数字
'嗨, 我有一个带有 2 位数字的字符串(如 32),但我想获取并将其用作 2 位数字(如 3 和 2)。我该怎么办?提前致谢。
NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:@"/"];
NSString *firstThird = [components objectAtIndex:0];
unichar firstChar = [firstThird characterAtIndex: 0];
NSString *firstCharStr = [NSString stringWithFormat:@"%c", firstChar];
unichar secChar = [firstThird characterAtIndex: 1];
NSString *secCharStr = [NSString stringWithFormat:@"%c",secChar];
NSLog(@"%@ over %@", firstCharStr, secCharStr);
if ([firstCharStr isEqualToString: @"1"]) {
firstCharStr=@"wet";
NSLog(@"wet");
}
if ([firstCharStr isEqualToString: @"2"]) {
firstCharStr=@"snow";
NSLog(@"snow");
}
if ([firstCharStr isEqualToString: @"3"]) {
firstCharStr=@"ice";
NSLog(@"ice");
}
if ([secCharStr isEqualToString: @"1"]) {
secCharStr=@"wet";
NSLog(@"wet");
}
if ([secCharStr isEqualToString: @"2"]) {
secCharStr=@"snow";
NSLog(@"snow");
}
if ([secCharStr isEqualToString: @"3"]) {
secCharStr=@"ice";
NSLog(@"ice");
}
NSString *secThird = [components objectAtIndex:1];
if ([secThird isEqualToString: @"1"]) {
secThird = @"wet";
NSLog(@"wet");
}
if ([secThird isEqualToString:@"2"]) {
secThird = @"snow";
NSLog(@"snow");
}
if ([secThird isEqualToString:@"3"]) {
secThird = @"ice";
NSLog(@"ice");
}
NSString *thirdThird = [components objectAtIndex:2];
if ([thirdThird isEqualToString: @"1"]) {
thirdThird = @"wet";
NSLog(@"wet");
}
if ([thirdThird isEqualToString:@"2"]) {
thirdThird = @"snow";
NSLog(@"snow");
}
if ([thirdThird isEqualToString:@"3"]) {
thirdThird = @"ice";
NSLog(@"ice");
}
dep.text = [NSString stringWithFormat:@"1/3 %@%@ 2/3 %@ 3/3 %@", firstCharStr,secCharStr, secThird, thirdThird];
它现在适用于数字(即 32),但如果我尝试只输入一位数字,我会收到错误......知道吗?谢谢
`Hi,
i have a string with a 2 digit numbers (like 32) but i want to get and use it as 2 single digit number (like 3 and 2). how can I do it? thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:@"%@", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:@"/"];
NSString *firstThird = [components objectAtIndex:0];
unichar firstChar = [firstThird characterAtIndex: 0];
NSString *firstCharStr = [NSString stringWithFormat:@"%c", firstChar];
unichar secChar = [firstThird characterAtIndex: 1];
NSString *secCharStr = [NSString stringWithFormat:@"%c",secChar];
NSLog(@"%@ over %@", firstCharStr, secCharStr);
if ([firstCharStr isEqualToString: @"1"]) {
firstCharStr=@"wet";
NSLog(@"wet");
}
if ([firstCharStr isEqualToString: @"2"]) {
firstCharStr=@"snow";
NSLog(@"snow");
}
if ([firstCharStr isEqualToString: @"3"]) {
firstCharStr=@"ice";
NSLog(@"ice");
}
if ([secCharStr isEqualToString: @"1"]) {
secCharStr=@"wet";
NSLog(@"wet");
}
if ([secCharStr isEqualToString: @"2"]) {
secCharStr=@"snow";
NSLog(@"snow");
}
if ([secCharStr isEqualToString: @"3"]) {
secCharStr=@"ice";
NSLog(@"ice");
}
NSString *secThird = [components objectAtIndex:1];
if ([secThird isEqualToString: @"1"]) {
secThird = @"wet";
NSLog(@"wet");
}
if ([secThird isEqualToString:@"2"]) {
secThird = @"snow";
NSLog(@"snow");
}
if ([secThird isEqualToString:@"3"]) {
secThird = @"ice";
NSLog(@"ice");
}
NSString *thirdThird = [components objectAtIndex:2];
if ([thirdThird isEqualToString: @"1"]) {
thirdThird = @"wet";
NSLog(@"wet");
}
if ([thirdThird isEqualToString:@"2"]) {
thirdThird = @"snow";
NSLog(@"snow");
}
if ([thirdThird isEqualToString:@"3"]) {
thirdThird = @"ice";
NSLog(@"ice");
}
dep.text = [NSString stringWithFormat:@"1/3 %@%@ 2/3 %@ 3/3 %@", firstCharStr,secCharStr, secThird, thirdThird];
it works now with to digits (ie 32) but if i try to put just one digit I get an error.... any idea? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此示例可以获取任何数字并将其转换为
NSNumber
数组。使用权:
This sample can take any number and convert it into an array of
NSNumber
s.Access:
NSString 的
-characterAtIndex:
应该可以解决问题:NSString's
-characterAtIndex:
should do the trick:循环遍历并一次从字符串中提取一个字符。
Loop through and extract one character at a time from your string.
跳过任何 unicode 复杂性,如何提取字符
unichar firstChar = [firstThird characterAtIndex: 0];
Skipping any unicode complexities, how about extracting the characters
unichar firstChar = [firstThird characterAtIndex: 0];