从字符串后删除字符和一切 [英] Remove Characters and Everything After from String

查看:118
本文介绍了从字符串后删除字符和一切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道替换字符串的字符串,但是只有我知道确切地知道我想要删除。

I am aware of replacing Strings of a String, but that only works if I know exactly what I want to remove.

如果我有一个字符串,如下所示:

If I have a String like the following:

hi-there-this-is-a-test& feature = hi-there

"hi-there-this-is-a-test&feature=hi-there"

如何删除& feature及其后的所有内容?

How do I remove '&feature' and everything that comes after that?

任何帮助将非常感谢。提前感谢!

Any help would be greatly appreciated. Thanks in advance!

编辑:如果绝对需要使用REGEX,有人可以告诉我如何使用它吗?我知道这是10.7起,但我很好。更好的是String修剪或使用NSScanner的示例?

If absolutely necessary to use REGEX, could someone show me how to use it? I am aware it is 10.7 onwards but I'm fine with that. Even better, an example of String trimming or using the NSScanner?

再次感谢大家。

解决方案贴在下面是正确的,但导致了我的崩溃。这是我解决的问题:

The solution posted below is the correct one, but resulted in a crash for me. This is how I solved the problem:

NSString *newString = [[oldString componentsSeparatedByString: @"&feature="] objectAtIndex:0];


推荐答案

p>

It can be done without REGEX like this:

NSString *string = @"hi-there-this-is-a-test&feature=hi-there";
    NSRange range = [string rangeOfString:@"&feature"];
    NSString *shortString = [string substringToIndex:range.location];

这篇关于从字符串后删除字符和一切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆