简体   繁体   English

如何知道一个字符串中的前五个字符

[英]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. 我有一个iPad应用程序,如果TextField.text以5个特定字符开头,它将做一件事,否则,如果它不是以5个特定字符开头,则将做不同的动作。 Any idea of how to know the 5 first characters of a NSString ? 是否知道如何知道NSString的前5个字符?

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])];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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