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);
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:#" | "];
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
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
How to get a substring from NSSTring from index 0 to 99. i.e. first 100 characters
[myString substringToIndex:100]
You need to be sure that index 100 is valid, i.e. length of string is at least 100. Otherwise an exception will be thrown.
[str substringToIndex: 100]
I have 2 NSArray's that are holding values...
For example NSArray 1 has values 1 2 4 in it
and NSArray 2 has values 1 2 4 5 6 in it.
How can I write code to compare these 2 arrays to get the following information...
Count the values that are the same (so in this case 3) and count the values that are not the same (in this case 2).
I am simply populating the arrays like this:
NSString *s = #"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:#","];
where *s is actually getting the text from a UITextField. If sorting mattering in comparing can you show me code to sort to make sure the user doesnt put the numbers in order?
If you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like intersectSet: and minusSet:
I would probably use the following method of the NSArray class:
enumerateObjectsUsingBlock.
and code the block testing for membership in the other array with the method:
indexOfObjectIdenticalTo.
If this isn't clear to you let me know.