0

I want to filter string after character '='. For eg if 8+9=17 My output should be 17. I can filter character before '=' using NSScanner, how to do its reverse??? I need a efficient way to do this without using componentsSeparatedByString or creating an array

Francis F
  • 3,157
  • 3
  • 41
  • 79
  • 2
    `componentsSeparatedByString` is (in *my* opinion) one of the most often abused methods. Why create an array? Isn't `rangeOfString`+`substringFromIndex` much more straighforward? But SO answerers and readers seem to like it! - Sorry, I had to say this once. – Martin R Aug 16 '13 at 14:06
  • Martin R is right, but I wasn't sure how rangeOfString option is better. I would recommend to accept @rmaddy's answer instead of mine, by pressing checkmark below the answer. – Nirav Bhatt Aug 16 '13 at 15:59
  • Can you explain why you are not comfortable with proposed solutions (e.g., using `componentsSeparatedByString`)? – Shai Aug 19 '13 at 05:07
  • It is quite inefficient when you just want one part of a string. The one I mark best answer describes this the best. And in my requirement i have a log like 8+9=+9-7=19 and I need to filter only 19 from the array of such logs. – Francis F Oct 04 '13 at 14:38
  • Try this NSArray* foo = [@"10/04/2011" componentsSeparatedByString: @"/"]; NSString* day = [foo objectAtIndex: 0]; – Suraj K Thomas Dec 19 '14 at 09:31

4 Answers4

13

Everyone seems to like to use componentsSeparatedByString but it is quite inefficient when you just want one part of a 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 is no = in the string");
}

Update:

Note - for this specific example, the difference in efficiencies is negligible if it is only being done once.

But in general, using componentsSeparatedByString: is going to scan the entire string looking for every occurrence of the delimiter. It then creates an array with all of the substrings. This is great when you need most of those substrings.

When you only need one part of a larger string, this is very wasteful. There is no need to scan the entire string. There is no need to create an array. There is no need to get all of the other substrings.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
8
NSArray * array = [string componentsSeparatedByString:@"="];
if (array)
{
    NSString * desiredString = (NSString *)[array lastObject]; //or whichever the index
}
else
{
    NSLog(@""); //report error - = not found. Of array could somehow be not created.
}

NOTE: Though this is very popular splitting solution, it is only worth trying whenever every substring separated by separator string is required. rmaddy's answer suggest better mechanism whenever the need is only to get small part of the string. Use that instead of this approach whenever only small part of the string is required.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • 1
    This is a very inefficient way to get just one little piece of a larger string. – rmaddy Aug 16 '13 at 15:45
  • This is simple and effective LOC. Sure for +1! – Jayprakash Dubey Jan 13 '14 at 11:20
  • check Array count then assign if ([array count]>1) { NSString * desiredString = (NSString *)[array objectAtIndex:1]; //or whichever the index } else { NSLog(@""); //report error - = not found. Of array could somehow be not created. } – shyla May 07 '15 at 12:27
5

Try to use this one

        NSArray *arr = [string componentsSeparatedByString:@"="];
        if (arr.count > 0)
        {
            NSString * firstString = [arr objectAtIndex:0];
            NSString * secondString = [arr objectAtIndex:1];
            NSLog(@"First String %@",firstString);
            NSLog(@"Second String %@",secondString);
        }

Output

First String 8+9

Second String 17

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Use this:

 NSString *a =@"4+6=10";
    NSLog(@"%@",[a componentsSeparatedByString:@"="])

;

 Log: Practice[7582:11303] (
        "4+6",
        10
    )
Krishna Kumar
  • 1,652
  • 9
  • 17