Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类似于Objective-C中Java中的.endsWith的函数_Objective C_Cocoa Touch - Fatal编程技术网

类似于Objective-C中Java中的.endsWith的函数

类似于Objective-C中Java中的.endsWith的函数,objective-c,cocoa-touch,Objective C,Cocoa Touch,我的数组中有一些字符串,可以是.html或.txt文件。在Java中,我们可以使用.endsWith(“String”)来检查一个字符串是否以另一个字符串结尾。在Objective-C中,我们如何实现同样的目标 例如:我想检查此字符串是否以.html或.txt结尾。因此字符串似乎是文件名,因此您可以使用: - (BOOL)hasSuffix:(NSString *)aString 如果要专门检查文件扩展名,请使用-pathExtension。如果没有,请使用rangeOfString:。请花点

我的数组中有一些字符串,可以是.html或.txt文件。在Java中,我们可以使用
.endsWith(“String”)
来检查一个字符串是否以另一个字符串结尾。在Objective-C中,我们如何实现同样的目标


例如:我想检查此字符串是否以.html或.txt结尾。

因此字符串似乎是文件名,因此您可以使用:

- (BOOL)hasSuffix:(NSString *)aString

如果要专门检查文件扩展名,请使用
-pathExtension
。如果没有,请使用
rangeOfString:
。请花点时间扫描文档中的
NSString
。你会很快找到答案的。谢谢,这条路很简单…@user2758888。。。。如果是一条路径,那么这个答案是不正确的。
NSString *extension = [filename pathExtension];
if ([extension isEqualToString:@"html"]) {
    // it's .html
} else if ([extension isEqualToString:@"txt"]) {
    // it's .txt
} else {
    // it's neither .html nor .txt
}