将字符转换为字符串

发布于 2024-11-07 08:27:22 字数 524 浏览 1 评论 0原文

我的代码工作得很好,直到知道,如果我在文本字段中输入一个两位数数字(如 12),nslog 将返回 2 个个位数数字(如 1 和 2)。现在我需要将这 2 个个位数放入 2 个字符串中。有人可以帮助我吗?提前致谢。

    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);
}

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

水溶 2024-11-14 08:27:22

您应该能够使用 -stringWithFormat:

NSString *s = [NSString stringWithFormat:@"%c", extractedChar];

编辑:

您可以将它们存储在数组中。

NSMutableArray *digits = [NSMutableArray array];

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

    [digits addObject:[NSString stringWithFormat:@"%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]];
}
葬心 2024-11-14 08:27:22

尝试使用 NSLog() 打印 firstThird 的值,看看它到底包含什么,您的代码似乎是正确的,

请使用 characterAtIndex 函数来 NSString 在已知位置提取字符

   - (unichar)characterAtIndex:(NSUInteger)index

使用如下

NSString *FirstDigit = [NSString stringWithFormat:@"%c", [myString characterAtIndex:0]];
NSString *SecondDigit  = [NSString stringWithFormat:@"%c", [myString characterAtIndex:1]];

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