提取一小段NSString

时间:2011-01-18 10:01:05

标签: iphone objective-c nsstring nsarray

我有一个数组,内容看起来像这样:

    "user/01453303276519456080/state/com.google/starred",
    "user/01453303276519456080/state/com.google/broadcast",
    "user/01453303276519456080/label/comics",
    "user/01453303276519456080/label/General News",
    "user/01453303276519456080/label/iPhone Related News",
    "user/01453303276519456080/label/Programming",
    "user/01453303276519456080/label/Sports",
    "user/01453303276519456080/label/Tech News",
    "user/01453303276519456080/label/Tutorials",
    "user/01453303276519456080/state/com.blogger/blogger-following"

我想只提取“/”之后的最后一个单词(例如一般新闻,编程等)用户/之后的唯一ID不会是常数。

有人可以给我一个实现这个想法吗?

干杯, ISEE

4 个答案:

答案 0 :(得分:3)

您可以在阵列的每个字符串上使用NSString的lastPathComponent API。

答案 1 :(得分:2)

查看lastPathComponent的{​​{1}}方法。这应该会给你你想要的。如果您还需要其他部分,则可以使用方法NSString或使用componentsSeperatedByString:

答案 2 :(得分:2)

使用此

for(int i=0;i<[yourArray count];i++)
{
  NSMutableArray *tempArray=[[[NSMutableArray alloc] init] autorelease];
  string=[yourArray objectAtIndex:i];       
  tempArray=[[string componentsSeparatedByString:@"/"] mutableCopy];
  //use tempArray

}

现在tempArray将所有字符串放在不同的索引处,根据你的

获取它们

答案 3 :(得分:-1)

for(int i=0;i<[myArray count];i++)
{
  NSMutableArray *tempArray=[[[NSMutableArray alloc] init] autorelease];
  string=[myArray objectAtIndex:i];
  NSArray *arr=[string componentsSeparatedByString:@"/"];
  tempArray=[arr objectAtIndex:([arr count]-1)];

}

现在这个tempArray包含你想要的最后一个组件

希望它能帮到你!

相关问题