i found this
Is it possible to use format strings to align NSStrings like numbers can be?
I have a similar task but the opposite format.
TOTAL: 2
SUBTOTAL: 6.00
TAX: 0%
CASH: 12.00
my code is this
NSString* item [NSString stringWithFormat:#"%-10s %#", #"TOTAL",#"2"];
Yet outcome is this
TOTAL: 2
SUBTOTAL: 6.00
UPDATE
I got it to work with this code.
NSString* item [NSString stringWithFormat:#"%12s %7s", [total UTF8String],[amount UTF8String]];
UPDATE QUESTION
What if the text is too long?
THIS TEXT IS TOO LONG
I want it to be
THIS TEXT
IS TOO LONG
Related
I currently have several arrays of strings which join together into one array of strings for a search. #"%# | %# | %# | %# | %# | %#". Now I am trying to separate the contents of the large array of strings, #"%#", back into the original six strings. Is there any way I can do this. Yes it sounds redundant but it is necessary at this point of development.
Edit: For clarification, I am using six parallel arrays and inserting each corresponding value between the pipes and converting it into a combined array of strings. I want to separate
"Word A | Word B | Word C | Word D | Word E | Word F", into
NSString A = "Word A";
NSString B = "Word B";
etc.
To separate this string you can use:
NSArray *array = [yourStrngToSeparate componentsSeparatedByString:#" | "];
If you want to put it back to string you don't have to use stringWithFormat, you can do:
NSString *string = [array componentsJoinedByString:#" | "];
In my application user enters price in text field. So I take the price from textfield like below
NSNumber *price = [NSNumber numberWithDouble:[[self stringAfterRemovingCurrencySymbol:self.priceTextField.text] doubleValue]];
When I print the value like below
DLog(#"Price Value 1: %f",[[self stringAfterRemovingCurrencySymbol:self.buyTextField.text] doubleValue]);
DLog(#"Price Value 2: %#",price);
Here is output (Lets say user enters 150.00)
Price Value 1: 150.000000
Price Value 2: 150
What I want is
Price Value 1: 150.000000
Price Value 2: 150.00
If user enters 150.85 , I am getting 150.85 as it is. But when user enters .00 , I am not getting .00
What should I do so that I will get the .00 ?
Thanks in advance.
You can get the NSNumber's value as a double and use standard printf formatting to specify precision:
DLog(#"Price Value 2: %.2f", [price doubleValue]);
I want to create a siple app which swaps the bytes of 2 and 4 byte hex codes.
So it should do: from 1234 to 3421 swap. I google and found out that I have to use byteorder and CFSwapInt32 and CFSwapInt16.
Here is what I already got:
NSString *byteOrder = [NSString stringWithFormat:#"%d",CFSwapInt32(12345678)];
NSLog(byteOrder);
But instead of the correct swapped bytes I get: 1315027968 as the number of the NSLog.
Can someone help me or tell me what I did wrong? :) I just want to swap bytes so they are in reversed order
1234 -->3412
12 34 -->34 12
12345678 -->78563412
12 34 56 78 --> 78 56 34 12
Thank you
Try
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x12345678)];
%x will output a value as hexadecimal.
Starting a number with 0x will interpret it as a hexadecimal value.
Your original number is 12345678 which, in hex, is 0x00BC614E
The output you get in the log is 1315027968 which, in hex, is 0x4E61BC00
So everything is working correctly.
You can try doing the same in hex if you prefer:
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x00BC614E)];
NSLog(byteOrder);
should log 0x4E61BC00 while
NSString *byteOrder = [NSString stringWithFormat:#"%x",CFSwapInt32(0x12345678)];
NSLog(byteOrder);
should log 0x78563412
How can i format my NSString to be something like
1 Item1 7.00 0% 7.00
1 Item2 6.00 0% 6.00
i'm getting the string from html it is in table format. Any ideas?
You can use IEEE printf flags to specify field widths (it's in the iOS library documentation). That means that if you have the content of the table cells as separate values or objects, you can create your fixed-space string using NSString's stringWithFormat: method.
I think, it solves your problem?
NSString *str = [NSString stringWithFormat:#"%i \t %# \t %.2f %%%i %.2f",1,#"Item1",7.00,0,6.00];
NSLog(#"%#", str);
I am using objective c, and I got some variables like this:
1100
920
845
1439
But I want to change it to :
11:00
09:20
08:45
14:39
The problem is how can I fill the zero when I got three number only? I know the logic can be simple to do it I detect the string length, but is there any way to do it more effectually? Thank you.
unsigned int time = 1100;
NSString *strTime = [NSString stringWithFormat:#"%02u:%02u", time / 100, time % 100];