Java Knowledge Accumulation 1-stringalign Implement text center right-to-left alignment

Source: Internet
Author: User

Import java.text.*;
public class Stringalign extends format{
public static final int just_left= ' l '; Left-to-Chi Chang volume
public static final int just_right= ' R '; Right to Chi Chang amount
public static final int just_center= ' C '; Center constant
private int just; The current pair of its
private int maxchars;
Public stringalign (int maxchars,int just) {
Switch (Just) {
Case Just_center:
Case Just_left:
Case Just_right:
This.just=just;
Break
Default
throw new IllegalArgumentException ("Invalid justification arg.");
}
if (maxchars<0) {
throw new IllegalArgumentException ("must be a positive number!") ");
}
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));
If there is no space for alignment, the contents of the maximum line width are returned
if (Wanted.length () >maxchars) {
Where.append (Wanted);
}
else switch (just) {
Case Just_right:
Pad (Where,maxchars-wanted.length ());
Where.append (Wanted);
Break
Case Just_left:
Where.append (Wanted);
Pad (Where,maxchars-wanted.length ());
Break
Case Just_center:
int Startpos=where.length ();
Pad (where, (Maxchars-wanted.length ())/2);
Where.append (Wanted);
Pad (where, (Maxchars-wanted.length ())/2);
Adjustment of the rounding error
Pad (where,maxchars-(where.length ()-startpos));
Break

}
return where;
}
protected final void pad (StringBuffer to,int howmany) {
for (int i=0;iTo.append (");
}
}
string format (string s) {
return format (s,new stringbuffer (), null). ToString ();
}
@Override
Public Object parseobject (String source, parseposition Pos) {
TODO auto-generated Method Stub
return source;
}

}

Java Knowledge Accumulation 1-stringalign Implement text center right-to-left alignment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.