删除 NSString 的一部分

发布于 2024-10-07 03:01:46 字数 334 浏览 3 评论 0 原文

我有一个 NSString 如下:

<img alt="996453912" src="http://d2gg0uigdtw9zz.cloudfront.net/large/996453912.jpg" /><a href="http://www.dealcatcher.com/shop4tech-coupons">Shop4Tech Coupons</a>

我只需要第一部分(在 部分之前),并且我不知道如何删除第二部分。

我已经尝试了很多,但没有成功。

I have an NSString as follows:

<img alt="996453912" src="http://d2gg0uigdtw9zz.cloudfront.net/large/996453912.jpg" /><a href="http://www.dealcatcher.com/shop4tech-coupons">Shop4Tech Coupons</a>

I only need the first part (before the <a href part), and I cannot figure out how to remove the second part.

I have tried a ton, but it has not worked.

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

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

发布评论

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

评论(3

心如荒岛 2024-10-14 03:01:46

使用类似的东西:

NSRange rangeOfSubstring = [string rangeOfString:@"<a href"];

if(rangeOfSubstring.location == NSNotFound)
{
     // error condition — the text '<a href' wasn't in 'string'
}

// return only that portion of 'string' up to where '<a href' was found
return [string substringToIndex:rangeOfSubstring.location];

所以两个相关的方法是 substringToIndex:rangeOfString:

Use something like:

NSRange rangeOfSubstring = [string rangeOfString:@"<a href"];

if(rangeOfSubstring.location == NSNotFound)
{
     // error condition — the text '<a href' wasn't in 'string'
}

// return only that portion of 'string' up to where '<a href' was found
return [string substringToIndex:rangeOfSubstring.location];

So the two relevant methods are substringToIndex: and rangeOfString:.

儭儭莪哋寶赑 2024-10-14 03:01:46

NSString 类参考中有一个关于 查找字符和子字符串其中列出了一些有用的方法。

在字符串编程指南中有一个关于 搜索、比较和排序字符串。

我并不是在指出这些链接。您说过您找不到方法,因此这里有一些参考资料可以帮助您知道在哪里寻找。学习如何阅读文档是学习如何使用 Cocoa 和 Cocoa-Touch 框架的一部分。

There is a section in the NSString Class reference about Finding Characters and Substrings which lists some helpful methods.

And in the String Programming Guide There is a section on Searching, Comparing and Sorting Strings.

I'm not being shirty in pointing out these links. You've said that you've couldn't find methods so here are a couple of references to help you know where to look. Learning how to read the documentation is part of learning how to use the Cocoa and Cocoa-Touch Frameworks.

谜泪 2024-10-14 03:01:46

您可以使用与此处发布的类似问题的答案类似的修改版本 https://stackoverflow.com/a /4886998/283412。这将获取您的 HTML 字符串并去掉格式。只需修改 while 部分即可删除要删除的正则表达式:

-(void)myMethod
{
   NSString* htmlStr = @"<some>html</string>";
   NSString* strWithoutFormatting = [self stringByStrippingHTML:htmlStr];
}

-(NSString *)stringByStrippingHTML:(NSString*)str
{
  NSRange r;
  while ((r = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
  {
     str = [str stringByReplacingCharactersInRange:r withString:@""];
  }
  return str;
}

You could use something similar to this modified version of what was posted as an answer to a similar question here https://stackoverflow.com/a/4886998/283412. This will take your HTML string and strip out the formatting. Just modify the while part to remove the regex of what you want to strip:

-(void)myMethod
{
   NSString* htmlStr = @"<some>html</string>";
   NSString* strWithoutFormatting = [self stringByStrippingHTML:htmlStr];
}

-(NSString *)stringByStrippingHTML:(NSString*)str
{
  NSRange r;
  while ((r = [str rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
  {
     str = [str stringByReplacingCharactersInRange:r withString:@""];
  }
  return str;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文