这些方法:
- 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());
}
}
运行截图: