ios - How to properly take out a string from a string? -
i have nsstring looks "alassetsgroup - name:hej - progcam, type:album, assets count:0" want "extract" name "hej". tried this:
nsstring *sstr = [nsstring stringwithformat:@"%@", assetgroups[indexpath.row]]; nsrange range1 = [sstr rangeofstring:@"name: "]; nsrange range2 = [sstr rangeofstring:@" - progcam"]; nsrange rrange = {range1.location + range1.length, range2.location}; nsstring *rstr = [sstr substringwithrange:rrange]; cell.celllabel.text = [nsstring stringwithformat:@"%@", rstr]; but error:
*** terminating app due uncaught exception 'nsrangeexception', reason: '-[__nscfstring substringwithrange:]: range or index out of bounds' what doing wrong here?
below should work. had use similar parse html screen scrapes. looking text in nsstring based on search characters should satisfied below. put beginning string first, componentsseparatedbystring @ index 0 terminating.
*note - returns first instance. if there other instances, they'll ignored in nsstring
nsstring *sstr = [nsstring stringwithformat:@"%@", assetgroups[indexpath.row]]; nsstring *name= [[[[sstr componentsseparatedbystring:@"name:"] objectatindex:1] componentsseparatedbystring:@" - progcam"] objectatindex:0]; cell.celllabel.text = [nsstring stringwithformat:@"%@", name];
Comments
Post a Comment