dynamic string format with NSString stringWithFormat

后端 未结 3 1622
一向
一向 2021-02-04 14:37

I\'ve been trying to make a dynamic string format so that through user options precision or padding could be somewhat user defined.

An example includes, the padding of

3条回答
  •  失恋的感觉
    2021-02-04 15:19

    I think you could build up your format in an entirely separate stage, then use that. Also, don't forget to escape one of the %'s to make it literal.

    NSString *format = [NSString stringWithFormat:@"%%%@i", padding];
    NSString *result = [NSString stringWithFormat:@"Hour: %@", format]; 
    

    update: oops maddy's answer pointed out a problem with mine: to escape a %, you should use %%, not \%. answer updated.

提交回复
热议问题