NSString - 2 位数字

发布于 2024-11-06 16:48:54 字数 1977 浏览 2 评论 0原文

'嗨, 我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放飞的风筝 2024-11-13 16:48:55
NSString *firstThird = [components objectAtIndex:0];//--- I get 32 from this string but i want 3 and 2
NSArray *numbersAsStrings = [firstThird componentsSeparatedByString:@""];
NSMutableArray *digits = [NSMutableArray array];
for (NSString *digit in numbersAsStrings)
{
    [digits addObject:[NSNumber numberWithInt:[digit intValue]]];
}

此示例可以获取任何数字并将其转换为 NSNumber 数组。

使用权:

int int1 = [[digits objectAtIndex:0]intValue]; //returns 3
NSString *firstThird = [components objectAtIndex:0];//--- I get 32 from this string but i want 3 and 2
NSArray *numbersAsStrings = [firstThird componentsSeparatedByString:@""];
NSMutableArray *digits = [NSMutableArray array];
for (NSString *digit in numbersAsStrings)
{
    [digits addObject:[NSNumber numberWithInt:[digit intValue]]];
}

This sample can take any number and convert it into an array of NSNumbers.

Access:

int int1 = [[digits objectAtIndex:0]intValue]; //returns 3
南笙 2024-11-13 16:48:54

NSString 的 -characterAtIndex: 应该可以解决问题:

NSString            *firstThird = @"32";
unichar             c1, c2;

c1 = [ firstThird characterAtIndex: 0 ];
c2 = [ firstThird characterAtIndex: 1 ];

switch ( c1 ) {
case '3':
    ...
}

NSString's -characterAtIndex: should do the trick:

NSString            *firstThird = @"32";
unichar             c1, c2;

c1 = [ firstThird characterAtIndex: 0 ];
c2 = [ firstThird characterAtIndex: 1 ];

switch ( c1 ) {
case '3':
    ...
}
聚集的泪 2024-11-13 16:48:54

循环遍历并一次从字符串中提取一个字符。

for(int i = 0; i < [myString length]; i++)
{
      char extractedChar = [myString characterAtIndex:i];

      if(extractedChar == '1')....
}

Loop through and extract one character at a time from your string.

for(int i = 0; i < [myString length]; i++)
{
      char extractedChar = [myString characterAtIndex:i];

      if(extractedChar == '1')....
}
初相遇 2024-11-13 16:48:54

跳过任何 unicode 复杂性,如何提取字符

unichar firstChar = [firstThird characterAtIndex: 0];

Skipping any unicode complexities, how about extracting the characters

unichar firstChar = [firstThird characterAtIndex: 0];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文