字符串格式化 +黑莓 +爪哇

发布于 2024-10-10 07:07:43 字数 759 浏览 0 评论 0原文

我在字符串格式方面遇到了一些问题。在我的应用程序中,我需要向网络服务发送一封电子邮件。电子邮件的格式需要如下所示

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2024-10-17 07:07:43

尝试使用 String.format()< /code>如果您使用的黑莓 jdk 版本中有此

package org.life.java.so.questions;

/**
 *
 * @author Jigar
 */
public class StringFormatDemo {
    public static void main(String[] args) {
        String name = "Jigar";
        String header = String.format("Name \t Class \t Section \t Positoin");
        String dataRow1 = String.format("%s \t %s \t %s \t %s",name,"A","IT","JavaDev");
        System.out.println(header);
        System.out.println(dataRow1);
    }
}

更新:

由于您的java环境不存在上述方法,您可以使用

javax.microedition.global.Formatter.formatMessage(...)

Try using String.format() if it is there in the version of jdk you are using with blackberry

package org.life.java.so.questions;

/**
 *
 * @author Jigar
 */
public class StringFormatDemo {
    public static void main(String[] args) {
        String name = "Jigar";
        String header = String.format("Name \t Class \t Section \t Positoin");
        String dataRow1 = String.format("%s \t %s \t %s \t %s",name,"A","IT","JavaDev");
        System.out.println(header);
        System.out.println(dataRow1);
    }
}

Update:

As the above method is not there with your java env you can go for

javax.microedition.global.Formatter.formatMessage(...)

清晰传感 2024-10-17 07:07:43

在我看来,您试图创建一个在查看器中看起来很好对齐的电子邮件正文。我猜,您使用的是比例字体(如 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 use String#size() instead for padding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文