如何选择从开头到指定字符的字符串?

发布于 2024-09-18 11:08:14 字数 486 浏览 5 评论 0原文

如何选择从开头到指定字符的字符串?

例如,在以下新闻标题中...

someString = @“加利福尼亚州洛杉矶 - Apple 宣布了一些事情,股票价格发生变化。”

如何仅选择 加利福尼亚州洛杉矶 - 成一个单独的字符串? (我想将我的选择基于 -(“破折号”)字符之前的所有内容。

编辑:

假设我的标题如下所示:

someString = @"Los加利福尼亚州洛杉矶 - Apple 宣布了一些事情 - 股票价格发生变化。”

如何防止我的位置字符串看起来像这样:加利福尼亚州洛杉矶 - Apple 宣布了一些事情

编辑:

我的错误,我删除了第一个破折号,然后重新输入了我的错误,发布的答案有效。

How can I select a string from the beginning until a specified character?

For example, in the following a news headline...

someString = @"Los Angeles, California - Apple announces something, stock prices change."

How do I select just Los Angeles, California - into a separate string? (I want to base my selection on everything before the - ("dash") character.

EDIT:

Say my headline looks like this:

someString = @"Los Angeles, California - Apple announces something - stock prices change."

How do I prevent my location string from looking like this: Los Angeles, California - Apple announces something?

Edit:

My mistake, I was removing the first dash and then reprising the string. My mistake the posted answer works.

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

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

发布评论

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

评论(1

稀香 2024-09-25 11:08:14
NSRange rangeOfDash = [someString rangeOfString:@"-"];
substring = (rangeOfDash.location != NSNotFound) ? [someString substringToIndex:rangeOfDash.location] : nil;

如果 someString 中没有破折号,这会将 substring 设置为 nil

NSRange rangeOfDash = [someString rangeOfString:@"-"];
substring = (rangeOfDash.location != NSNotFound) ? [someString substringToIndex:rangeOfDash.location] : nil;

This sets the substring to nil if there's no dash in the someString.

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