자바에서 문자열의 좌/우 정렬
.
문자열인 텍스트를 자바에서 좌측, 우측, 중앙 정렬하기 위해서는 별도의 클래스를 구현해서 이용해야 하는데 그 예가 아래의 StringAlign.java 입니다.
별도로 만들어진 StringAlign.java라는 텍스트 정렬용 클래스는 Formatter 클래스를 상속하여 문자열의 정렬을 구현했습니다.
-------------------
StringAlign.java
------------------
import java.text.*;
/** Bare-minimum String formatter (string aligner). */
public class StringAlign extends Format {
/* Constant for left justification. */
public static final int JUST_LEFT = 'l';
/* Constant for centering. */
public static final int JUST_CENTRE = 'c';
/* Centering Constant, for those who spell "centre" the American way. */
public static final int JUST_CENTER = JUST_CENTRE;
/** Constant for right-justified Strings. */
public static final int JUST_RIGHT = 'r';
/** Current justification */
private int just;
/** Current max length */
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;
}
/** Format a String */
public StringBuffer format(
Object obj, StringBuffer where, FieldPosition ignore) {
String s = (String)obj;
String wanted = s.substring(0, Math.min(s.length(), maxChars));
// If no space left for justification, return maxChars' worth */
if (wanted.length() > maxChars) {
where.append(wanted);
}
// Else get the spaces in the right place.
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);
// Adjust for "rounding error"
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(' ');
}
/** Convenience Routine */
String format(String s) {
return format(s, new StringBuffer(), null).toString();
}
/** ParseObject is required, but not useful here. */
public Object parseObject (String source, ParsePosition pos) {
return source;
}
}
-----------------------
StringAlignDemo.java
-----------------------
public class StringAlignDemo {
/** Demonstrate and test StringAlign class */
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();
}
}
[결과]
---------- java ----------
Input String "JavaFun"
l[5] ==> "JavaF"
l[10] ==> "JavaFun "
c[5] ==> "JavaF"
c[10] ==> " JavaFun "
r[5] ==> "JavaF"
r[10] ==> " JavaFun"
Input String "JavaFun!"
l[5] ==> "JavaF"
l[10] ==> "JavaFun! "
c[5] ==> "JavaF"
c[10] ==> " JavaFun! "
r[5] ==> "JavaF"
r[10] ==> " JavaFun!"
.
문자열인 텍스트를 자바에서 좌측, 우측, 중앙 정렬하기 위해서는 별도의 클래스를 구현해서 이용해야 하는데 그 예가 아래의 StringAlign.java 입니다.
별도로 만들어진 StringAlign.java라는 텍스트 정렬용 클래스는 Formatter 클래스를 상속하여 문자열의 정렬을 구현했습니다.
-------------------
StringAlign.java
------------------
import java.text.*;
/** Bare-minimum String formatter (string aligner). */
public class StringAlign extends Format {
/* Constant for left justification. */
public static final int JUST_LEFT = 'l';
/* Constant for centering. */
public static final int JUST_CENTRE = 'c';
/* Centering Constant, for those who spell "centre" the American way. */
public static final int JUST_CENTER = JUST_CENTRE;
/** Constant for right-justified Strings. */
public static final int JUST_RIGHT = 'r';
/** Current justification */
private int just;
/** Current max length */
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;
}
/** Format a String */
public StringBuffer format(
Object obj, StringBuffer where, FieldPosition ignore) {
String s = (String)obj;
String wanted = s.substring(0, Math.min(s.length(), maxChars));
// If no space left for justification, return maxChars' worth */
if (wanted.length() > maxChars) {
where.append(wanted);
}
// Else get the spaces in the right place.
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);
// Adjust for "rounding error"
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(' ');
}
/** Convenience Routine */
String format(String s) {
return format(s, new StringBuffer(), null).toString();
}
/** ParseObject is required, but not useful here. */
public Object parseObject (String source, ParsePosition pos) {
return source;
}
}
-----------------------
StringAlignDemo.java
-----------------------
public class StringAlignDemo {
/** Demonstrate and test StringAlign class */
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();
}
}
[결과]
---------- java ----------
Input String "JavaFun"
l[5] ==> "JavaF"
l[10] ==> "JavaFun "
c[5] ==> "JavaF"
c[10] ==> " JavaFun "
r[5] ==> "JavaF"
r[10] ==> " JavaFun"
Input String "JavaFun!"
l[5] ==> "JavaF"
l[10] ==> "JavaFun! "
c[5] ==> "JavaF"
c[10] ==> " JavaFun! "
r[5] ==> "JavaF"
r[10] ==> " JavaFun!"
'Programming > JAVA' 카테고리의 다른 글
파일 이름 변경 (0) | 2008.04.28 |
---|---|
유니코드와 문자열의 변환 (0) | 2008.04.28 |
실행 시 Stack Trace 얻기 (0) | 2008.04.28 |
WebLogic서버 감시 데몬 (0) | 2008.04.28 |
싱글톤(Singleton)을 만들자~~ (0) | 2008.04.28 |