字符串格式化 +黑莓 +爪哇
我在字符串格式方面遇到了一些问题。在我的应用程序中,我需要向网络服务发送一封电子邮件。电子邮件的格式需要如下所示
Name Class Section Position
Sam 5 A 1
Joseph 7 C 4
为此,我使用 /n 和 /t 作为换行符和间距。但真正的问题是“名称”项。 “名称”项的长度各不相同。目前我的方法是,我采用足够长的引用字符串,并在每个名称字符串中填充空格,直到其长度等于基本字符串。
我面临的问题是,当我附加空格时,这种方法无法正常工作,但如果我附加任何其他字符,如“x”,则生成的字符串格式正确,而不是空格。
附加空格的函数:
private String getModifiedName(String name){
String testString = "This is a very big string";
while(getFont().getAdvance(testString) > getFont().getAdvance(name)){
name = name + " ";
}
return name;
}
I am facing a bit problem with formatting of string. In my application I need to send an email to a web service.The format of the email need to be like this
Name Class Section Position
Sam 5 A 1
Joseph 7 C 4
For this I have used /n and /t for line breaks ans spacing. But the real problem is with the 'Name' item. The length of 'name' item varies. Currently my approach is that I am taking a reference string sufficiently long and padding blank spaces in each name string until its length is equal to the base String.
The problem that I am facing is that this approach doesn't work fine when I append blank spaces but instead of blank spaces if I append any other character say 'x' then the resulting string is properly formatted.
Function for appending blank spaces :
private String getModifiedName(String name){
String testString = "This is a very big string";
while(getFont().getAdvance(testString) > getFont().getAdvance(name)){
name = name + " ";
}
return name;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
String.format()< /code>
如果您使用的黑莓 jdk 版本中有此
更新:
由于您的java环境不存在上述方法,您可以使用
javax.microedition.global.Formatter.formatMessage(...)
Try using
String.format()
if it is there in the version of jdk you are using with blackberryUpdate:
As the above method is not there with your java env you can go for
javax.microedition.global.Formatter.formatMessage(...)
在我看来,您试图创建一个在查看器中看起来很好对齐的电子邮件正文。我猜,您使用的是比例字体(如 times 或 helvetica/arial)。
Web 服务通常不关心表看起来是否对齐,而是关心消息是否对齐。确保每列的字符数相同,即使结果在屏幕上看起来很难看。因此,确实不应该使用
getFont().getAdvance()
方法来使字符串(几乎)大小相等,而是使用String#size()
来填充。It seems to me that you try to create an email body that looks well aligned in a viewer. And I guess, you're using a proportional font (like times or helvetica/arial).
Webservices usually don't care if the table looks aligned but care if the message is aligned. Make sure, that the number of chars is each column is the same, even if the result looks ugly on the screen. So really shouldn't use the
getFont().getAdvance()
method to make Strings (almost) equal size but useString#size()
instead for padding.