nsstring - How to filter a string after a particular character in iOS? -
this question has answer here:
- split nsstring access 1 particular piece 7 answers
i want filter string after character '='. eg if 8+9=17 output should 17. can filter character before '=' using nsscanner, how reverse??? need efficient way without using componentsseparatedbystring or creating array
everyone seems use componentsseparatedbystring quite inefficient when want 1 part of string.
try this:
nsstring *str = @"8+9=17"; nsrange equalrange = [str rangeofstring:@"=" options:nsbackwardssearch]; if (equalrange.location != nsnotfound) { nsstring *result = [str substringfromindex:equalrange.location + equalrange.length]; nslog(@"the result = %@", result); } else { nslog(@"there no = in string"); } update:
note - specific example, difference in efficiencies negligible if being done once.
but in general, using componentsseparatedbystring: going scan entire string looking every occurrence of delimiter. creates array of substrings. great when need of substrings.
when need 1 part of larger string, wasteful. there no need scan entire string. there no need create array. there no need of other substrings.
Comments
Post a Comment