从 NSString 获取最后一个路径部分

发布于 2024-11-16 13:06:36 字数 127 浏览 1 评论 0原文

大家好,我想从字符串中提取最后一部分,这是一个四位数字“03276”,即:http://www.abc.com/news/read/welcome-new-gig/03276

我怎样才能做到这一点。

Hi all i want extract the last part from string which is a four digit number '03276' i:e http://www.abc.com/news/read/welcome-new-gig/03276

how can i do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夏日浅笑〃 2024-11-23 13:06:36

您还可以使用

NSString *sub = [@"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent];

You can also use

NSString *sub = [@"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent];
我们只是彼此的过ke 2024-11-23 13:06:36

如果你知道需要多少个字符,你可以这样做:

NSString *string = @"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [string substringFromIndex:[string length] - 5];

如果你只知道这是最后一个斜杠之后的部分,你可以这样做:

NSString *string = @"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [[string componentsSeparatedByString:@"/"] lastObject];

If you know how many characters you need, you can do something like this:

NSString *string = @"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [string substringFromIndex:[string length] - 5];

If you just know that it's the part after the last slash, you can do this:

NSString *string = @"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *subString = [[string componentsSeparatedByString:@"/"] lastObject];
月牙弯弯 2024-11-23 13:06:36

由于 *nix 使用与 URL 相同的路径分隔符,这也将有效。

[@"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent]

Since *nix uses the same path separators as URL's this will be valid as well.

[@"http://www.abc.com/news/read/welcome-new-gig/03276" lastPathComponent]
时光瘦了 2024-11-23 13:06:36

如果您知道数字的长度,并且它不会改变,那么它可以很简单:

NSString *result = [string substringFromIndex:[string length] - 4];

If you know the length of the number, and it's not gonna change, it can be as easy as:

NSString *result = [string substringFromIndex:[string length] - 4];
初心 2024-11-23 13:06:36

如果字符串的最后部分始终具有相同的长度(5 个字符),您可以使用此方法来提取最后部分:

- (NSString *)substringFromIndex:(NSUInteger)anIndex

使用字符串的长度来确定起始索引。

像这样的:(

NSString *inputStr = @"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *newStr = [inputStr substringFromIndex:[inputStr length]-5];
NSLog(@"These are the last five characters of the string: %@", newStr);

代码未测试)

If the last part of the string is always the same length (5 characters) you could use this method to extract the last part:

- (NSString *)substringFromIndex:(NSUInteger)anIndex

Use the length of the string to determine the start index.

Something like this:

NSString *inputStr = @"http://www.abc.com/news/read/welcome-new-gig/03276";
NSString *newStr = [inputStr substringFromIndex:[inputStr length]-5];
NSLog(@"These are the last five characters of the string: %@", newStr);

(Code not tested)

乜一 2024-11-23 13:06:36
NSString *str = @"http://www.abc.com/news/read/welcome-new-gig/03276";  
NSArray *arr = [str componentSeparatedBy:@"gig/"];  
NSString *strSubStringDigNum = [arr objectAtIndex:1];  

strSubStringDigNum 的值为 03276

NSString *str = @"http://www.abc.com/news/read/welcome-new-gig/03276";  
NSArray *arr = [str componentSeparatedBy:@"gig/"];  
NSString *strSubStringDigNum = [arr objectAtIndex:1];  

strSubStringDigNum will have the value 03276

笑看君怀她人 2024-11-23 13:06:36

试试这个:

NSString *myUrl = @"http://www.abc.com/news/read/welcome-new-gig/03276";

NSString *number = [[myUrl componentsSeparatedByString:@"/"] objectAtIndex: 5];

Try this:

NSString *myUrl = @"http://www.abc.com/news/read/welcome-new-gig/03276";

NSString *number = [[myUrl componentsSeparatedByString:@"/"] objectAtIndex: 5];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文