How do I check if a string contains another string in Swift?

后端 未结 27 3840
天命终不由人
天命终不由人 2020-11-22 12:32

In Objective-C the code to check for a substring in an NSString is:

NSString *string = @\"hello Swift\";
NSRange textRange =[strin         


        
27条回答
  •  甜味超标
    2020-11-22 13:06

    You don't need to write any custom code for this. Starting from the 1.2 version Swift has already had all the methods you need:

    • getting string length: count(string);
    • checking if string contains substring: contains(string, substring);
    • checking if string starts with substring: startsWith(string, substring)
    • and etc.

提交回复
热议问题