NSString 搜索子字符串/ csv 文件 IO
这让我非常头疼。无论出于何种原因,当我使用这段代码时,if 语句的计算结果总是为 false:
while(!feof(file))
{
NSString *line = [self readNSString:file];
NSLog(@"%@", line);
NSLog(@"%@", search);
NSRange textRange;
textRange =[line rangeOfString:search];
if(textRange.location != NSNotFound)
{
NSString *result = [line substringFromIndex:NSMaxRange([line rangeOfString:search])];
resultView.text = result;
}
else
{
resultView.text = @"Not found";
}
}
当函数执行时,两个 NSLog 告诉我“line”和“search”字符串应该是什么,那么为什么 if语句总是评估为假?我一定错过了一些简单的事情,如果有另一双眼睛那就太好了。谢谢
编辑:(函数“readNSString”)
- (NSString*)readNSString:(FILE*) file
{
char buffer[300];
NSMutableString *result = [NSMutableString stringWithCapacity:256];
int read;
do
{
if(fscanf(file, "%299[^\n]%n%*c", buffer, &read) == 1)
[result appendFormat:@"%s", buffer];
else
break;
} while(r == 299);
return result;
}
编辑2:
通过调用第一个函数来设置搜索,并以 NSString* 变量作为参数,如下所示:
NSString *textFieldText = [[NSString alloc]
initWithFormat:@"%@", textField.text];
[self readFile:textFieldText];
编辑 3(NSLogs 输出)
行:德国意大利法国
搜索:意大利
This has given me quite a big headache. For whatever reason, when I use this code, the if statement always evaluates to false:
while(!feof(file))
{
NSString *line = [self readNSString:file];
NSLog(@"%@", line);
NSLog(@"%@", search);
NSRange textRange;
textRange =[line rangeOfString:search];
if(textRange.location != NSNotFound)
{
NSString *result = [line substringFromIndex:NSMaxRange([line rangeOfString:search])];
resultView.text = result;
}
else
{
resultView.text = @"Not found";
}
}
When the functions execute, the two NSLogs tell me that the "line" and "search" strings are what they should be, so then why does the if statement always evaluate to false? I must be missing something simple, having another set of eyes would be great. Thanks
edit: (function "readNSString")
- (NSString*)readNSString:(FILE*) file
{
char buffer[300];
NSMutableString *result = [NSMutableString stringWithCapacity:256];
int read;
do
{
if(fscanf(file, "%299[^\n]%n%*c", buffer, &read) == 1)
[result appendFormat:@"%s", buffer];
else
break;
} while(r == 299);
return result;
}
edit 2:
search is set with a call to the first function, with an NSString* variable as a parameter, like this:
NSString *textFieldText = [[NSString alloc]
initWithFormat:@"%@", textField.text];
[self readFile:textFieldText];
edit 3 (NSLogs output)
line: Germany Italy France
search: Italy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正确使用
rangeOfString
和NSNotFound
等,因此问题可能与使用从文件读取的数据创建字符串有关appendFormat:@"%s"
。我怀疑您的两种字符串格式之间可能存在编码问题 - 我将调查“%s”是否将空终止的 C 字符串正确编码为与具有适当编码的 unicode
NSString
相同的格式。尝试将从 readNSString 函数获取的值硬编码为代码中的字符串文字,以进行测试,看看该比较是否有效,如果有效,则表明它可能与从文件创建的字符串的编码。
I think that you are using the
rangeOfString
and theNSNotFound
etc. correctly, so the problem is possibly to do with the creation of the string from the data read from the file using theappendFormat:@"%s"
.I suspect there may be an encoding issue between your two string formats - I would investigate whether the "%s" encodes the null terminated C string properly into the same format as a unicode
NSString
with the appropriate encoding.Try hard coding the value you are getting from the
readNSString
function as a string literal in code just for testing and see if that comparison works, if so this would tend to indicate it probably is something to do with the encoding of the string created from the file.