Java—String类扩展功能实现

//String类扩展功能实现
public class Strings{
    
     /**
     * 1.重复某个字符
     * 
     * 例如: 
     * 'a' 5   => "aaaaa"  
     * 'a' -1  => ""
     * 
     * @param c     被重复的字符
     * @param count 重复的数目,如果小于等于0则返回""
     * @return 重复字符字符串
     */
    public static String repeat(char c, int count) {
       StringBuffer sb = new StringBuffer();//定义StringBuffer类便于字符串内容的修改
		if(count >0){
			for(int i = 0;i "AAabc"
     * "abc" 'A' 3  => "abc"
     *
     * @param str        被填充的字符串
     * @param filledChar 填充的字符
     * @param len        填充长度
     * @return 填充后的字符串
     */
    public static String fillBefore(String str, char filledChar, int len) {
       if(len>0)
	   {
		 char[] c = new char[len];//定义要填充的字符数组
		 for(int i = 0;i
     * 字符填充于字符串后
     * 例如: 
     * "abc" 'A' 5  => "abcAA"
     * "abc" 'A' 3  => "abc"
     *
     * @param str        被填充的字符串
     * @param filledChar 填充的字符
     * @param len        填充长度
     * @return 填充后的字符串
     */
    public static String fillAfter(String str, char filledChar, int len) {
		char[] c=str.toCharArray();
		int n = len-c.length;
		if(c.length aabbccdd
     *
     * @param str         字符串
     * @param strToRemove 被移除的字符串
     * @return 移除后的字符串
     */
       public static String removeAll(CharSequence str, CharSequence strToRemove) {//定义字符串,被移除的字符串
		String str1=str.toString();//将字符序列转变为String类
		String str2=strToRemove.toString();
		String strs="";
        char[] c=str1.toCharArray();//将字符串转变为字符数组
		char[] c2=str2.toCharArray();
		char c1=strToRemove.charAt(0);
		for(int i=0;i dcba
     *
     * @param str 被反转的字符串
     * @return 反转后的字符串
     */
        public static String reverse(String str) {
		
        char[] c = str.toCharArray();//将String类转化为字符数组
		int left = 0;
		int right = c.length - 1;//定义左右指针,遍历逆序摆放
		while(left

你可能感兴趣的:(java)