从 NSString 中删除 http://
如何从 NSString
中删除某些文本,例如“http://”?它需要完全按照这个顺序。感谢您的帮助!
这是我正在使用的代码,但是 http:// 并未删除。相反,它会显示 http://http://www.example.com。我应该怎么办?谢谢!
NSString *urlAddress = addressBar.text;
[urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
urlAddress = [NSString stringWithFormat:@"http://%@", addressBar.text];
NSLog(@"The user requested this host name: %@", urlAddress);
How do I remove certain text from a NSString
such as "http://"? It needs to be exactly in that order. Thanks for your help!
Here is the code I am using, however the http:// is not removed. Instead it appears http://http://www.example.com. What should I do? Thanks!
NSString *urlAddress = addressBar.text;
[urlAddress stringByReplacingOccurrencesOfString:@"http://" withString:@""];
urlAddress = [NSString stringWithFormat:@"http://%@", addressBar.text];
NSLog(@"The user requested this host name: %@", urlAddress);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
像这样?
(如果您只想删除开头的文本,请执行 jtbandes 所说的操作 - 上面的代码也将替换字符串中间出现的文本)
Like this?
(if you want to remove text at the beginning only, do what jtbandes says - the code above will replace occurrences in the middle of the string as well)
这是一个处理 http 和 http 的解决方案。 https:
Here's a solution which takes care of http & https:
如果 http:// 位于字符串的开头,您可以使用
或者按照 SVD 建议
编辑:看到问题后编辑
将此行更改
为
if http:// is at the start of the string you can use
or else as SVD suggested
EDIT: AFter seeing question EDIT
change this line
to
另一种方法是:
Another way is :
由于该线程仍然处于活动状态并出现在我的搜索中,以从 URL(不是 NSString)中删除前缀...如果您从 URL 开始,则会有一行:
Since the thread is still active and showed up in my search to remove the prefix from a URL (not NSString)... there is a one-liner if you are starting with a URL:
这是另一种选择;
Here is another option;
如果您希望修剪两侧并编写更少的代码:
此代码将从前面删除指定的前缀并从后面删除后缀(如尾部斜杠)。只需向前缀/后缀数组添加更多子字符串即可过滤更多内容。
In case you wish to trim both sides and also write less code:
This code will remove specified prefixes from the front and suffixes from the back (like a trailing slash). Simply add more substrings to the prefix/suffix array to filter for more.
Swift 3
用于替换所有出现的位置:
用于替换字符串开头的出现位置:
Swift 3
For replacing all occurrences:
For replacing occurrences at the start of the string:
对于那些使用 swift 并已经到达这里的人。
For those using swift and have arrived here.
此字符串扩展添加了一个
removeHTTPPrefix()
方法,该方法通过拆分字符串从 URL 中删除http://
或https://
前缀位于://
并返回其后的部分,如果未找到分隔符则返回原始字符串:This String extension adds a
removeHTTPPrefix()
method that removes thehttp://
orhttps://
prefix from a URL by splitting the string at://
and returning the part after it, or the original string if no separator is found:或者
OR
NSString* newString = [string stringByReplacingOccurrencesOfString:@"http://" withString:@""];
NSString* newString = [string stringByReplacingOccurrencesOfString:@"http://" withString:@""];
一种更通用的方法:
One more general way:
大家好,有点晚了,但我有一个通用的方法
比方说:
Hi guys bit late but I come with a generic way
Let's say:
这将删除任何方案,包括 http、https 等。
This will remove any scheme including http, https, etc.