skip to Main Content

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


  1. Something like below

    NSString *combined = [NSString stringWithFormat:@"%@%@", stringURL, stringSearch];
    if (combined.length < 16)
    {
        NSString *newCombined = [NSString stringWithFormat:@"%@%@", combined, @"Some new string"];
    }
    

    You can use substringWithRange: method from NSString. You can take the below code as an example and modify it as per your requirements.

    if (combined.length > 25)
    {
        NSString *beginning = [combined substringWithRange:NSMakeRange(0, 15)];
        NSString *fromEnd = [combined substringWithRange:NSMakeRange(startPoint, combined.length-startPoint)];
    }
    
    Login or Signup to reply.
  2. You could make use of stringWithFormat – basically of printf 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.

            // To get 50 spaces
            NSString * s50 = [NSString stringWithFormat:@"%*s", 50, ""];
    
            // Pad with these characters, select only 1
    
            // This will pad with spaces
            char * pad = "";
    
            // This will pad with minuses - you need enough to fill the whole field
            char * pad = "-------------------------------------------------------";
    
            // Some string
            NSString * s = @"Hi there";
    
            // Here back and front are just int's. They must be, but they can be calculated,
    
            // e.g. you could have this to pad to 50
            int back = 50 - s.length; if ( back < 0 ) back = 0;
    
            // Pad s at the back
            int back = 20;
            NSString * sBack = [NSString stringWithFormat:@"%@%*s", s, back, pad];
    
            // Pad s in front
            int front = 10;
            NSString * sFront = [NSString stringWithFormat:@"%*s%@", front, pad, s];
    
            // Pad s both sides
            NSString * sBoth = [NSString stringWithFormat:@"%*s%@%*s", front, pad, s, back, pad];
    

    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 as n is an int and you can use that to then perform calculations, store it in n and pad. There is an example in the code.

    Here is a sample of the output

    2020-11-04 08:16:22.908828+0200 FormatSpecifiers[768:15293] [Hi there-------------------------------------------------------]
    2020-11-04 08:16:22.908931+0200 FormatSpecifiers[768:15293] [-------------------------------------------------------Hi there]
    2020-11-04 08:16:22.908992+0200 FormatSpecifiers[768:15293] [-------------------------------------------------------Hi there-------------------------------------------------------]
    

    I just show how to pad the combined string. To combine the string of course just use stringByAppendingString e.g.

    NSString * s = [a stringByAppendingString:b];
    

    and then you can do calcs based on s.length e.g. as shown in the example.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search