In iOS I am using two strings to combine and to form a single string. But the combined string must be of 16 characters . So if two strings are small and if we combine both and if it is less than 16 characters I must add few more characters to make it to 16 characters. How to achieve this?
NSString *combined = [NSString stringWithFormat:@"%@%@", stringURL, stringSearch];
This is the code I am using. So if I combine and it is less than 16 characters how to calculate it and add more characters to make it 16 characters?
2
Answers
Something like below
You can use
substringWithRange
: method fromNSString
. You can take the below code as an example and modify it as per your requirements.You could make use of
stringWithFormat
– basically ofprintf
if you want to pad with just a single char. Below I give some examples which I have constructed to illustrate, so it won’t run out the box, but you only need to comment out the ones you do not want to make it work.Note that the amounts here are parameterised. I use e.g. 50 in the first line but that could just as well be
n
as long asn
is an int and you can use that to then perform calculations, store it inn
and pad. There is an example in the code.Here is a sample of the output
I just show how to pad the combined string. To combine the string of course just use
stringByAppendingString
e.g.and then you can do calcs based on
s.length
e.g. as shown in the example.