µ±Ç°Î»Ö㺠´úÂëÃÔ >> J2SE >> javaÖÐÓÐÀàËÆsqlÖÐrpadÕâ¸ö·½·¨µÄÂ𣿸ÃÈçºÎ´¦Àí
  Ïêϸ½â¾ö·½°¸

javaÖÐÓÐÀàËÆsqlÖÐrpadÕâ¸ö·½·¨µÄÂ𣿸ÃÈçºÎ´¦Àí

Èȶȣº105   ·¢²¼Ê±¼ä£º2016-04-24 14:00:18.0
javaÖÐÓÐÀàËÆsqlÖÐrpadÕâ¸ö·½·¨µÄÂð£¿
ÈçÌ⣬JAVAÖÐÓÐÀàËÆÕâÖÖ×Ö·û´®ÓÒ²¹ÆëµÄ·½·¨Âð£¿

PS£º
rpad(var_name,5, 'a ');
µ±var_name   =   'bb 'µÄʱºò£¬·µ»ØÖµÎª 'bbaaa ';
µ±var_name   =   'bbb 'µÄʱºò£¬·µ»ØÖµÎª 'bbbaa ';

ÇëÅóÓÑÃÇÖ¸µãÏ£¬Ð»Ð»¡£

------½â¾ö·½°¸--------------------
×Ô¼ºÐ´°É£¡
public String rpad(String str,int num,String pad){
String n_str=str;
if(str==null)
n_str= " ";
for(int i=str.length();i <num;i++){
n_str+=pad;
}
return n_str;
}
------½â¾ö·½°¸--------------------
StringBufferµÄinsert·½·¨ºÍÕâ¸ö²î²»¶à°É
------½â¾ö·½°¸--------------------
ûÓУº
ÓÃÕâ¸ö°É
package com.capitalnd.util;

import java.text.*;

/**
* StringPadder¤Ï¤Ò¤â¤Ë×ó¤Î¥Ñ¥Ã¥É¤ª¤è¤ÓÓҤΥѥåɤΙCÄÜÐÔ¤òÌṩ¤¹¤ë
* @author penglee
* <br> 2007-07-03
*/
public class StringPadder {
private static StringBuffer strb;
private static StringCharacterIterator sci;

/** method to left pad a string with a given string to a given size. This
* method will repeat the padder string as many times as is necessary until
* the exact specified size is reached. If the specified size is less than the size of
* the original string then the original string is returned unchanged.
* Example1 - original string "cat ", padder string "white ", size 8 gives "whitecat ".
* Example2 - original string "cat ", padder string "white ", size 15 gives "whitewhitewhcat ".
* Example3 - original string "cat ", padder string "white ", size 2 gives "cat ".
* @return String the newly padded string
* @param stringToPad The original string
* @param padder The string to pad onto the original string
* @param size The required size of the new string
*/
public static String leftPad (String stringToPad, String padder, int size)
{
if (padder.length() == 0)
{
return stringToPad;
}
strb = new StringBuffer(size);
sci = new StringCharacterIterator(padder);

while (strb.length() < (size - stringToPad.length()))
{
for (char ch = sci.first(); ch != CharacterIterator.DONE ; ch = sci.next())
{
if (strb.length() < size - stringToPad.length())
{
strb.insert( strb.length(),String.valueOf(ch));
}
}
}
return strb.append(stringToPad).toString();
}

/** method to right pad a string with a given string to a given size. This
* method will repeat the padder string as many times as is necessary until
* the exact specified size is reached. If the specified size is less than the size of
* the original string then the original string is returned unchanged.
* Example1 - original string "cat ", padder string "white ", size 8 gives "catwhite ".
* Example2 - original string "cat ", padder string "white ", size 15 gives "catwhitewhitewh ".
* Example3 - original string "cat ", padder string "white ", size 2 gives "cat ".
* @return String the newly padded string
* @param stringToPad The original string
* @param padder The string to pad onto the original string
* @param size The required size of the new string
*/
public static String rightPad (String stringToPad, String padder, int size)
  Ïà¹Ø½â¾ö·½°¸