开发者

How can I add leading whitespace in front of the NSString?

开发者 https://www.devze.com 2023-02-15 00:50 出处:网络
I need the String always have leng开发者_C百科th 5... for example, if I pass hello\", I get \"hello\"

I need the String always have leng开发者_C百科th 5... for example, if

I pass hello", I get "hello"

I pass "xml", I get " xml" (two spaces before "xml");

I pass "i", I get " i" (four spaces before "i").

What is the most efficient way to do it? Thank you.


I suppose this might help:

int maxLength = 5;
NSString *text = @"xml";
NSString *result = [text stringByPaddingToLength:(maxLength-[text length]) withString:@" " startingAtIndex:0];


NSMutableString *yourString;//your string
NSMutableString *string;
if([string length]<5){
   NSInteger length = 5-[string length];
     for(int i=0;i<length;i++){
        [string appendFormat:@" "];
       }
[yourString appendString:string];

    }

//now you can use yourString .it is formatted.

0

精彩评论

暂无评论...
验证码 换一张
取 消