简体   繁体   中英

How to know which are the 5 first characters in a string

I have an iPad app, where, if the TextField.text starts with 5 specific characters, it will do one thing, and else if it doesn't start with it, will do a different action. Any idea of how to know the 5 first characters of a NSString ?

Use this:

if ([someString hasPrefix:@"hello"]) { ... }

Or, if you want to find out what the first five characters are instead of comparing against a specific string, you can do this instead:

NSString *firstFive = [someString substringToIndex:MIN(5, [someString length])];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM