iOS Objective-C 宏判断字符串是文本还是链接
您可以使用正则表达式来判断一个字符串是文本还是链接。以下是一个封装成宏的示例代码:\n\nobject-c\n#define IS_TEXT_OR_LINK(string) ([string rangeOfString:@"http://" options:NSCaseInsensitiveSearch] != NSNotFound || [string rangeOfString:@"https://" options:NSCaseInsensitiveSearch] != NSNotFound)\n\n// 使用示例\nNSString *str1 = @"This is a text.";\nNSString *str2 = @"https://www.example.com";\n\nif (IS_TEXT_OR_LINK(str1)) {\n NSLog(@"str1 is a link.");\n} else {\n NSLog(@"str1 is a text.");\n}\n\nif (IS_TEXT_OR_LINK(str2)) {\n NSLog(@"str2 is a link.");\n} else {\n NSLog(@"str2 is a text.");\n}\n\n\n上述代码使用了rangeOfString:options:方法来搜索字符串中是否包含http://或https://,如果找到则表示是链接,否则表示是文本。NSCaseInsensitiveSearch选项表示搜索时不区分大小写。\n\n请注意,这只是一个简单的示例代码,如果您需要更复杂的链接判断逻辑,可能需要使用更为强大的正则表达式或其他方法。
原文地址: https://www.cveoy.top/t/topic/qjjn 著作权归作者所有。请勿转载和采集!