Get last path part from NSString

前端 未结 7 1067
梦毁少年i
梦毁少年i 2020-12-13 19:20

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

7条回答
  •  不思量自难忘°
    2020-12-13 19:27

    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)

提交回复
热议问题