Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6031315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:09:55+00:00 2026-05-23T05:09:55+00:00

I simply have this expression in objective c NSString: @10+5*2. I wanted to solve

  • 0

I simply have this expression in objective c NSString: @”10+5*2″.
I wanted to solve this expression automatically and came across to a solution:

UIWebView *webView = [[UIWebView alloc] init];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:expression];
NSLog(@"%@",result); 

The above works well but it produces a result: 20.
Actually this evaluates the “*” first means first it does 5*2 = 10 + 10.
But I want to remove this precedence and simply wants each operator having same precedence with left to right so that answer could be 10+5 = 15 * 2 = 30.

Although writing own function is the last option but I really don’t want to mess-up with the string functions. I would like to use something built in.

Thanks,

******* Working solution ********

Thanks for all your help Eiko and Ozair!!
Based on their answers I have written this bunch of code which works perfectly for me!

NSString *expression = lblBox.text; // Loads the original expression from label.
    expression = [expression stringByReplacingOccurrencesOfString:@"+" withString:@")+"];
    expression = [expression stringByReplacingOccurrencesOfString:@"-" withString:@")-"];
    expression = [expression stringByReplacingOccurrencesOfString:@"*" withString:@")*"];
    expression = [expression stringByReplacingOccurrencesOfString:@"/" withString:@")/"];


    NSUInteger count = 0, length = [expression length];
    NSRange range = NSMakeRange(0, length); 

    while(range.location != NSNotFound)
    {
        range = [expression rangeOfString: @")" options:0 range:range];
                 if(range.location != NSNotFound)
                 {
                     range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
                     count++; 
                 }
    }

    for (int i=0; i<count; i++) {
        expression = [@"(" stringByAppendingString:expression];
    }


    UIWebView *webView = [[UIWebView alloc] init];
    NSString *result = [webView stringByEvaluatingJavaScriptFromString:expression];
    [webView release];
    NSLog(@"%@ = %@",expression, result);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:09:56+00:00Added an answer on May 23, 2026 at 5:09 am

    Not sure if it’s the same as @Ozair Kafray says…

    For each operator that you append to the string, add a ) first and add the matching ( at the beginning. I.e.

    10
    (10 ) +
    (10 ) + 5
    ((10 ) + 5 ) *
    ((10 ) + 5 ) * 2
    

    Just make sure that when deleting an operator to also delete the parentheses as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this regular expression in objective-c NSString* searchString = [searchBlock stringByReplacingOccurrencesOfString:@<(.*?)> withString:@ options:
I have the following expression: ^\w(\s(+|-|/|*)\s\w)*$ This simply looks to match a mathematical expression,
I have this small class called City that simply holds some information about a
I have a windows form simply like this: 1) a button when clicked will
I have a data that looks like this . And my code below simply
I have no clue how to validate this string. I am simply supplying an
I have the following bit of code, simply: $(function() { $('a.add-photos-link').live('click', function(e) { $(this).colorbox({
I basically have three tables, posts, images and postimages (this simply contains the ids
I'm trying to learn xQuery coming from a php background, I have this expression
I have a Silverlight application that I am trying to style. This style simply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.