简体   繁体   English

Objective-C子串

[英]Objective-C Substring

I'd like to know how to grab everything inbetween http:// and / 我想知道如何获取http:// and /

NOTE: The IP address will change depending on environments and it may be a hostname instead of an IP address. 注意:IP地址将根据环境而变化,它可能是主机名而不是IP地址。 I need to be able to handle both. 我需要能够处理这两个问题。

eg, 例如,

http://127.0.0.1/foo/bar/chocolate

Output: 输出:

http://127.0.0.1

That looks like a URL. 看起来像一个URL。 Why not use something like 为什么不使用类似的东西

[[NSURL URLWithString:theString] host]

That will return the hostname part of the URL, which in your example is 127.0.0.1 . 这将返回URL的主机名部分,在您的示例中为127.0.0.1 If you actually need a full URL that contains just the host, you could also try something like 如果您实际上需要仅包含主机的完整URL,则还可以尝试类似

[[NSURL URLWithString:"/" relativeToURL:[NSURL URLWithString:theString]] absoluteURL]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM