如何使用\t [tab]运算符格式化列中的NSString? [英] How can I use the \t [tab] operator to format a NSString in columns?

查看:141
本文介绍了如何使用\t [tab]运算符格式化列中的NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种格式化NSString的方法:

I would like to find a way to format a NSString like this:

Name:               John
Location:           Unknown
Type:               Unknown
Status:             Unknown

现在我正在使用这个代码可以做到,但结果完全不同:

Right now I'm using this code to do it but the result is quite different:

代码:

serializedValue = [serializedValue stringByAppendingFormat:@"%@:\t\t%@\n",title, value];

其中 title 是左栏和 value 是第二个。

Where title is the left column and value is the second one.

结果:

Name:       John
Location:       Unknown
Type:       Unknown
Status:     Unknown

现在,正如你所看到的,在我的情况下,总是添加一定数量的标签(\ t),2,这样做并不成功,因为左列较长,有些较短。我想知道是否有一种简单的方法可以做到这一点,我不知道。

Now, as you can see, adding always a constant number of tabs (\t), 2 in my case, doesn't do the trick, because some words in the left column are longer and some are shorter. I'm wondering if there is a simple way to do this that I'm not aware of.

推荐答案

NSString *titleColumn = [[NSString stringWithFormat:@"%@:", title] stringByPaddingToLength:20 withString:@" " startingAtIndex:0];
serializedValue = [serializedValue stringByAppendingFormat:@"%@%@", titleColumn, value];

顺便说一下,使用 NSMutableString for serializedValue

Btw, it would be more efficient to use an NSMutableString for serializedValue.

这篇关于如何使用\t [tab]运算符格式化列中的NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆