1

Sorry guys, I'm very new to web dev. I have the below string :

Mark sent $USD 100 to Olivia.

I'm getting this sentence in a UIWebView. So the string is essentially below:

NSString whatHappened = [NSString stringWithFormat: @"Mark sent <b>%@</b> to %@", amount, receiver];

NSString receiver = "Olivia"
NSString amount = "$USD 100"

Now, I basically want it to be so that, the $USD 100 is reversed, so it is: 100 $USD. How can I possibly achieve this? I have multiple things to send too, such as 100 Apples etc., so a simply reverse on the amount string won't work. Any suggestions on how I can change this in the HTML string?

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
  • I just happened to stumble across this question (I'm not sure how) and I'm not familiar with objective C, so I'll add a little to question: Does [NSString stringWithFormat: @"Mark sent %@ %@ to %@", type, amount, receiver]; not work, so you could have NSString receiver="Olivia" NSString amount="100" and NSString type="$USD" ? Again, not familiar. Apologize if this is a terrible comment. – Helpful Apr 18 '14 at 02:47
  • lol... no I cannot break it up. It's a server response that I get. – gran_profaci Apr 18 '14 at 02:49
  • Ah, makes sense. Like I said, unfamiliar (though I plan on learning). Thanks for your understanding. :) – Helpful Apr 18 '14 at 02:50
  • Can you post the code that reverses the amount string? Since you have said, '...a simply reverse on the amount string won't work.', then that part might be problematic. – mownier Apr 18 '14 at 03:40
  • I'm assuming a simple string reverse. – gran_profaci Apr 18 '14 at 22:09

2 Answers2

1

You have three good options:

1) Regular expressions (Search through NSString using Regular Expression)

2) just divide the string on the space:

NSArray *components = [string componentsSeparatedByString:@" "];
NSString *currency = components[0];
NSInteger amount = [components[1] integerValue];

3) use NSScanner (https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSScanner_Class/Reference/Reference.html)

Community
  • 1
  • 1
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110
0

In your case, you could use NSNumberFormatter to convert the amount back into an NSNumber and reformat it that way.

NSString *formatString = @"$USD 0.0";
NSNumberFormatter *currencyFormatter = [[NSNumberFormatter alloc] init];
[currencyFormatter setPositiveFormat:formatString];

NSNumber *amountValue = [currencyFormatter numberFromString:amount];

Then you can reformat it however you like:

if (amountValue != nil) {
    NSString *formattedValue = [NSString stringWithFormat:@"%@ $USD", amountValue];
}

Note that amountValue will equal nil if the string was not in the format that you specified to the formatter. This means that if you decide to send, for example, @"100 Apples" instead, you can easily detect it.

Your only challenge then would be identifying what the format of the incoming amount will be, if it has any likelihood of changing.

ttton
  • 586
  • 5
  • 7