如何检查字符串中的某些字符?

发布于 2024-11-05 14:34:16 字数 118 浏览 4 评论 0原文

我有一个字符串..

我正在使用该字符串输入用户的一些字符。

现在我想检查该字符串中“a”是否存在?

谁能告诉我该怎么做?

“a”可以位于字符串中的任何位置。

i have a string..

I am entering some characters from user using that string.

Now i want to check that in that string "a" is there or not?

can anyone tell me how to do this?

that "a" can be anywhere in the string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-11-12 14:34:16
if ([myString rangeOfString:@"a"].location != NSNotFound) {
  // "a" IS in myString
} else {
  // "a" ISN'T in myString
}

if ([myString rangeOfString:@"a"].location != NSNotFound && [myString rangeOfString:@"b"].location != NSNotFound) {
  // "a" AND "b" are BOTH in myString
} else  if ([myString rangeOfString:@"a"].location != NSNotFound) {
  // ONLY "a" is in myString
} else  if ([myString rangeOfString:@"b"].location != NSNotFound) {
  // ONLY "b" is in myString
} else {
  // Neither "a" NOR "b" is in myString
}
if ([myString rangeOfString:@"a"].location != NSNotFound) {
  // "a" IS in myString
} else {
  // "a" ISN'T in myString
}

if ([myString rangeOfString:@"a"].location != NSNotFound && [myString rangeOfString:@"b"].location != NSNotFound) {
  // "a" AND "b" are BOTH in myString
} else  if ([myString rangeOfString:@"a"].location != NSNotFound) {
  // ONLY "a" is in myString
} else  if ([myString rangeOfString:@"b"].location != NSNotFound) {
  // ONLY "b" is in myString
} else {
  // Neither "a" NOR "b" is in myString
}
琉璃梦幻 2024-11-12 14:34:16
- (BOOL)isCharaterExist:(NSString *)sPhrase withSearchChar:(NSString *)sChar {

      NSAssert(sPhrase != nil && sChar != nil, @"sPhrase and sChar should not be nil");

      return ([sPhrase rangeOfString:sChar].length > NSNotFound);
}

注意:未在运行环境中进行测试,因此可能存在拼写错误!

- (BOOL)isCharaterExist:(NSString *)sPhrase withSearchChar:(NSString *)sChar {

      NSAssert(sPhrase != nil && sChar != nil, @"sPhrase and sChar should not be nil");

      return ([sPhrase rangeOfString:sChar].length > NSNotFound);
}

Note: It's not tested in running environment, so typo mistakes can be possible!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文