Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 【原创】JAVA经典实例--字符串的对齐
prettyxx





发贴: 75
积分: 42
于 2003-12-18 14:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
问题:如何使字符串左对齐、右对齐或者居中
解决之道:
在这里,我们编写了一个基于java.text.Format类的StringAlign类
代码:
StringAlign类源代码:
import java.text.*;

/** 袖珍字符串格式化器(字符串对齐) */
public class StringAlign extends Format {
/* 用于左对齐的常量 */
public static final int JUST_LEFT = 'l';
/*用于居中的常量 */
public static final int JUST_CENTRE = 'c';
/* 居中常量(美式英语拼写为"centre") */
public static final int JUST_CENTER = JUST_CENTRE;
/**用于右对齐的常量 */
public static final int JUST_RIGHT = 'r';

/** 当前对齐位置 */
private int just;
/** 当前最大的行宽(以字符为单位) */
private int maxChars;

public StringAlign(int maxChars, int just) {
switch(just) {
case JUST_LEFT:
case JUST_CENTRE:
case JUST_RIGHT:
this.just = just;
break;
default:
throw new IllegalArgumentException("invalid justification arg.");
}
if (maxChars < 0) {
throw new IllegalArgumentException("maxChars must be positive.");
}
this.maxChars = maxChars;
}

/** 格式化字符串 */
public StringBuffer format(
Object obj, StringBuffer where, FieldPosition ignore) {

String s = (String)obj;
String wanted = s.substring(0, Math.min(s.length(), maxChars));

// 如果没有空间用于对齐,则分会最大行宽(maxChars)的内容 */
if (wanted.length() > maxChars) {
where.append(wanted);
}
// 否则,可得到右边空间的空格数
else switch (just) {
case JUST_RIGHT:
pad(where, maxChars - wanted.length());
where.append(wanted);
break;
case JUST_CENTRE:
int startPos = where.length();
pad(where, (maxChars - wanted.length())/2);
where.append(wanted);
pad(where, (maxChars - wanted.length())/2);
// 调整“取整误差”
pad(where, maxChars - (where.length() - startPos));
break;
case JUST_LEFT:
where.append(wanted);
pad(where, maxChars - wanted.length());
break;
}
return where;
}

protected final void pad(StringBuffer to, int howMany) {
for (int i=0; i<howMany; i++)
to.append(' ');
}

/** 子程序 */
String format(String s) {
return format(s, new StringBuffer(), null).toString();
}

/** ParseObject是必需的,但在这里并没有多大用处 */
public Object parseObject (String source, ParsePosition pos) {
return source;
}

}

主程序1:
/* 在行宽为70个字符的页面上打印一个页号 */
public class StringAlignSimple {

public static void main(String[] args) {
// 构造一个格式化器(formatter)用于字符串居中
StringAlign formatter = new StringAlign(70, StringAlign.JUST_CENTER);
// 测试第i页
System.out.println(formatter.format("- i -"));
// 测试第4页,由于这个格式化器是针对字符串的
// 而不是处理页号(int型)
//因而,需要将数字转化为字符串
System.out.println(formatter.format(Integer.toString(4)));
}
}


主程序2:

public class StringAlignDemo {

/** 测试StringAlign类 */
public static void main(String[] argv) {
String[] mesg = {"JavaFun", "JavaFun!" };
for (int i=0; i<mesg.length; i++) {
System.out.println("Input String \"" + mesg[i] + "\"");
dump(StringAlign.JUST_LEFT, 5,
new StringAlign(5, StringAlign.JUST_LEFT).format(mesg[i]));
dump(StringAlign.JUST_LEFT, 10,
new StringAlign(10, StringAlign.JUST_LEFT).format(mesg[i]));
dump(StringAlign.JUST_CENTER, 5,
new StringAlign(5, StringAlign.JUST_CENTER).format(mesg[i]));
dump(StringAlign.JUST_CENTER, 10,
new StringAlign(10, StringAlign.JUST_CENTER).format(mesg[i]));
dump(StringAlign.JUST_RIGHT, 5,
new StringAlign(5, StringAlign.JUST_RIGHT).format(mesg[i]));
dump(StringAlign.JUST_RIGHT, 10,
new StringAlign(10, StringAlign.JUST_RIGHT).format(mesg[i]));
}
}

private static void dump(int format, int len, String s) {
System.out.print((char)format + "[" + len + "]");
System.out.print(" ==> \"");
System.out.print(s);
System.out.print('"');
System.out.println();
}
}



我的Blog:阿泠的自省札记
========心有多大,舞台就有多大========

话题树型展开
人气 标题 作者 字数 发贴时间
5286 【原创】JAVA经典实例--字符串的对齐 prettyxx 5340 2003-12-18 14:59

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923