在Java库中提供了StringBuilder类。给出你自己对下面方法的实现(将新类命名为MyStringBuilder1)

这些方法:

  • public MyStringBuilder1(String s)
  • public MyStringBuilder1 append(MyStringBuilder1 s)
  • public MyStringBuilder1 append(int i)
  • public int length()
  • public char charAt(int index)
  • public MyStringBuilder1 toLowerCase()
  • public MyStringBuilder1 substring(int begin, int end)
  • public String toString()

代码在这:

public class MyStringBuilder1{
	private char[] value;
	public char[] show() {
		return value;
	}
	public MyStringBuilder1(String s) {
		value = new char[s.length()];
        for(int i=0; i<s.length(); i++) {
            value[i] = s.charAt(i);
        }
	}
	public MyStringBuilder1 append(MyStringBuilder1 s) {
		String string1 = new String();
		String string2 = new String();
		string1=String.valueOf(this.value);
		string2=String.valueOf(s.show());
		MyStringBuilder1 newString = new MyStringBuilder1(string1.concat(string2));
		return newString;
	}
	public MyStringBuilder1 append(int i) {
		String string1 = new String();
		String string2 = new String();
		string1=String.valueOf(value);
		string2=String.valueOf(i);
		MyStringBuilder1 newString = new MyStringBuilder1(string1.concat(string2));
		return newString;
	}
	public int length() {
		return this.value.length;
	}
	public char charAt(int index) {
		return value[index-1];
	}
	public MyStringBuilder1 toLowerCase(){
		String string = new String();
		string=String.valueOf(value);
		MyStringBuilder1 newString=new MyStringBuilder1(string.toLowerCase());
		return newString;
	}
	public MyStringBuilder1 substring(int begin, int end) {
		String string = new String();
		string=String.valueOf(value);
		MyStringBuilder1 newString = new MyStringBuilder1(string.substring(begin, end));
		return newString;
	}
	public String toString() {
		return new String(value);
	}
}

测试程序:
public class homeWork4 {
	public static void main(String[] args) {
		String s1 = new String();
		s1="Java Text ";
		String s2 = new String();
		s2="String Demo ";
		MyStringBuilder1 string1 = new MyStringBuilder1(s1);
		MyStringBuilder1 string2 = new MyStringBuilder1(s2);
		System.out.println(string1.append(string2).show());
		System.out.println(string2.append(1234).show());
		System.out.println(string1.length());
		System.out.println(string2.charAt(3));
		System.out.println(string1.toLowerCase().show());
		System.out.println(string2.substring(0, 3).show());
		System.out.println(string1.toString());
	}
}

运行截图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值