iphonesdk安装函数

iphonesdk安装函数,iphone,cocoa,Iphone,Cocoa,我正在尝试编写一个方法,从URI字符串中提取ID。下面的代码似乎效率低下,有什么更好的方法来查找id字符串 NSString *aID = [[[NSString alloc] initWithString:@"http://www.example.com/id368907840?mt=8&uo=4"] autorelease]; NSArray *arrStr = [aID componentsSeparatedByString:@"/id"]; aID = [arrStr objec

我正在尝试编写一个方法,从URI字符串中提取ID。下面的代码似乎效率低下,有什么更好的方法来查找id字符串

NSString *aID = [[[NSString alloc] initWithString:@"http://www.example.com/id368907840?mt=8&uo=4"] autorelease];
NSArray *arrStr = [aID componentsSeparatedByString:@"/id"];
aID = [arrStr objectAtIndex:1];
arrStr = [aID componentsSeparatedByString:@"?mt="];
aID = [arrStr objectAtIndex:0];
以上内容留给我的是NSString=368907840,您可以使用、和来完成与以下示例中相同的任务:

NSURL* url = [NSURL URLWithString:@"http://www.example.com/id368907840?mt=8&uo=4"];
NSString* lastpath = [url lastPathComponent]; // "id368907840"
NSCharacterSet* letters = [NSCharacterSet letterCharacterSet];
NSString* result = [lastpath stringByTrimmingCharactersInSet:letters]; // 368907840
免责声明:我实际上还没有尝试过,所以您需要自己尝试,但我相信这应该会奏效。

您可以使用、、和来完成与以下示例相同的任务:

NSURL* url = [NSURL URLWithString:@"http://www.example.com/id368907840?mt=8&uo=4"];
NSString* lastpath = [url lastPathComponent]; // "id368907840"
NSCharacterSet* letters = [NSCharacterSet letterCharacterSet];
NSString* result = [lastpath stringByTrimmingCharactersInSet:letters]; // 368907840

免责声明:我实际上还没有尝试过,所以您需要自己尝试,但我相信这应该会起作用。

您可以使用subStringWithRange和rangeOfString的组合,类似于

NSString *aID = [[[NSString alloc] initWithString:@"http://www.example.com/id368907840?mt=8&uo=4"] autorelease];

int startIndex = [aID rangeOfString:@"id"].location + 2;
int length = [aID rangeOfString:@"mt"].location - startIndex - 1;

aID = [aID substringWithRange:NSMakeRange(startIndex, length)];

您可以使用subStringWithRange和rangeOfString的组合,类似于

NSString *aID = [[[NSString alloc] initWithString:@"http://www.example.com/id368907840?mt=8&uo=4"] autorelease];

int startIndex = [aID rangeOfString:@"id"].location + 2;
int length = [aID rangeOfString:@"mt"].location - startIndex - 1;

aID = [aID substringWithRange:NSMakeRange(startIndex, length)];

使用正则表达式。其中一种情况是使用它们更容易、更可读。

使用正则表达式。其中一种情况是使用它们更容易、更可读。

谢谢我将其与:lastpath=[lastpath substringfromfromindex:2];//“368907840”谢谢我将此与:lastpath=[lastpath substringfromfromindex:2];//“368907840”我不认为可可中有正则表达式?我不认为可可中有正则表达式?