【问题标题】:How to search for a string including spaces in Objective C?如何在Objective C中搜索包含空格的字符串?
【发布时间】:2011-01-13 08:40:29
【问题描述】:

我有一个真正的基本命令行程序,在 Objective-C 中,用于搜索用户输入的信息。不幸的是,代码只会读取用户输入的一系列单词中的第一个单词。例如,如果用户输入 “Apples are great”,则只保留 “Apples”(因此稍后会搜索),不包括 “are great " 句子的一部分。

这是我目前所拥有的:

char enteredQuery [128]; // array 'name' to hold the scanf string
NSString *searchQuery; // ending NSString to hold and compare the user inputed data

NSLog(@"Enter search query:");
scanf("%s", enteredQuery); //will read the next line

searchQuery = [NSString stringWithCString: enteredQuery encoding: NSASCIIStringEncoding]; //converts scanf data into a NSString type

我知道这与我使用 scanf 或字符编码器转换有关,但我似乎无法弄清楚。非常感谢解决问题的任何帮助!谢谢。

【问题讨论】:

    标签: objective-c string character-encoding nsstring scanf


    【解决方案1】:

    scanf 读取模式 "%s" 的单个单词。您可以循环使用它。使用fgets 从标准输入读取可能会更好。请参阅herehere

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-12
      • 2011-03-11
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多