字符串格式+黑莓+ java [英] String Formatting + Blackberry + java

查看:213
本文介绍了字符串格式+黑莓+ java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在格式化字符串方面遇到了一些问题。在我的应用程序中,我需要向网络服务发送电子邮件。电子邮件的格式必须是这样的

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

为此我使用/ n和/ t表示换行符和间距。但真正的问题在于名称项目。 名称项目的长度各不相同。目前我的方法是我将一个足够长的引用字符串和每个名称字符串中的空格填充,直到它的长度等于基本字符串。

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.

我的问题我面临的是,当我附加空格而不是空格而不是空格时,如果我追加任何其他字符说'x',则结果字符串格式正确。

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.

附加空格的函数:

  private String getModifiedName(String name){
         String testString  = "This is a very big string";

         while(getFont().getAdvance(testString) > getFont().getAdvance(name)){
             name = name + " ";

         }



         return name;

     }


推荐答案

尝试使用 String.format() 如果它与您使用黑莓的jdk版本相同

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);
    }
}




  • IdeOne演示

    • IdeOne Demo
    • 更新:

      由于上述方法不适用于您的java环境,您可以选择

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

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

      这篇关于字符串格式+黑莓+ java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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