Ios 使用字符串分隔的组件从url获取值
我有一个如下所示的url: 我希望能够实现以下目标:Ios 使用字符串分隔的组件从url获取值,ios,objective-c,Ios,Objective C,我有一个如下所示的url: 我希望能够实现以下目标: section = image; value = ghwUT23Y; 我尝试分解url,并使用componentsOperatedByString方法获取所需的值 if ([[explodedUrl firstObject] containsString:@"google.com"] && ![[explodedUrl firstObject] isEqualToString:@"https://google.com
section = image;
value = ghwUT23Y;
我尝试分解url,并使用componentsOperatedByString
方法获取所需的值
if ([[explodedUrl firstObject] containsString:@"google.com"] && ![[explodedUrl firstObject] isEqualToString:@"https://google.com/"]) {
NSString *sectionString = [[[explodedUrl lastObject] componentsSeparatedByString:@"/"] firstObject];
NSLog(@"redirectttttt: %@",sectionString);
NSString *itemString = [[[explodedUrl lastObject] componentsSeparatedByString:@"/"] lastObject];
NSLog(@"redirectttttt:2 %@",itemString);
问题:
使用此方法,itemString
返回值:ghwUT23Y.jpeg
但sectionString
返回值:https://
如何才能将
图像
作为一个部分 Swift:
func detectUrl() {
let strLongDescription = "https://google.com/image/ghwUT23Y.jpeg"
if(strLongDescription.contains("image")){
var arrString : [String] = strLongDescription.components(separatedBy: "google.com/")
let lastOfarrString : String = arrString.last!
arrString = lastOfarrString.components(separatedBy: "/")
let section = arrString.first!
let value = arrString.last!
}
}
Swift:
func detectUrl() {
let strLongDescription = "https://google.com/image/ghwUT23Y.jpeg"
if(strLongDescription.contains("image")){
var arrString : [String] = strLongDescription.components(separatedBy: "google.com/")
let lastOfarrString : String = arrString.last!
arrString = lastOfarrString.components(separatedBy: "/")
let section = arrString.first!
let value = arrString.last!
}
}
您需要首先将字符串转换为url,并使用该方法访问该url的每个组件 目标C: 敏捷的
您需要首先将字符串转换为url,并使用该方法访问该url的每个组件 目标C: 敏捷的
我将使用
NSURLComponents
获取url的组件。可能重复NSURL*url=[NSURL URLWithString:@”https://google.com/image/ghwUT23Y.jpeg"]; NSArray*组件=[url路径组件];NSString*imageSection=[components objectAtIndex:1];NSString*imageName=[[components lastObject]stringByDeletingPathExtension]代码>应该可以做到这一点。我使用了直接索引,但应根据需要进行修改。请检查:NSString*url=@”https://google.com/image/ghwUT23Y.jpeg"; 如果([url rangeOfString:@“image”].location!=NSNotFound){NSArray*组件=[url ComponentSeparatedByString:@”/“];NSString*节=组件[components.count-2];NSString*值=[components lastObject];NSLog(@%@%,节,值)}
@Larme工作得很好,thanksI将使用NSURLComponents
获取url的组件。可能重复NSURL*url=[NSURL URLWithString:@”https://google.com/image/ghwUT23Y.jpeg"]; NSArray*组件=[url路径组件];NSString*imageSection=[components objectAtIndex:1];NSString*imageName=[[components lastObject]stringByDeletingPathExtension]代码>应该可以做到这一点。我使用了直接索引,但应根据需要进行修改。请检查:NSString*url=@”https://google.com/image/ghwUT23Y.jpeg"; 如果([url rangeOfString:@“image”].location!=NSNotFound){NSArray*组件=[url ComponentSeparatedByString:@”/“];NSString*节=组件[components.count-2];NSString*值=[components lastObject];NSLog(@“%@%@”,节,值);}
@Larme工作得很好,谢谢
let url = URL(string: "https://google.com/image/ghwUT23Y.jpeg")
var components = url?.pathComponents
let fileName = components?.popLast()
let section = components?.popLast()