Objective-C:子串和替换 [英] Objective-C: Substring and replace
本文介绍了Objective-C:子串和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 NSString
,它是一个 URL.这个网址需要剪掉:
I have a NSString
which is a URL. This URL need to be cut:
NSString *myURL = @"http://www.test.com/folder/testfolder";
NSString *test = [myURL stringByReplacingCharactersInRange:[myURL rangeOfString:@"/" options:NSBackwardsSearch] withString:@""];
我有这个 URL http://www.test.com/folder/testfolder
并且我希望 test
变量应该具有值 http://www.test.com/folder/
,所以testfolder
应该被剪掉.所以我试图找到 NSRange
testfolder
用空字符串替换它.
I have this URL http://www.test.com/folder/testfolder
and I want that the test
variable should have the value http://www.test.com/folder/
, so the testfolder
should be cut.
So I tried to find the NSRange
testfolder
to replace it with an empty string.
但它不起作用.我做错了什么?
But it does not work. What I am doing wrong?
推荐答案
你可以把它变成一个 URL 并使用 -[NSURL URLByDeletingLastPathComponent]
:
You can turn it into a URL and use -[NSURL URLByDeletingLastPathComponent]
:
NSString *myURLString = @"http://www.test.com/folder/testfolder";
NSURL *myURL = [NSURL URLWithString:myURLString];
myURL = [myURL URLByDeletingLastPathComponent];
myURLString = [myURL absoluteString];
这篇关于Objective-C:子串和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文