1.代码:
package yrx;
public class MyString {
/**
* The maximal length.
*/
public static final int MAX_LENGTH = 10;
/**
* The actual length.
*/
int length;
/**
* The data.
*/
char[] data;
/**
******************
* Construct an empty char array
******************
*/
public MyString() {
length = 0;
data = new char[MAX_LENGTH];// 创建一个字符数组
}// Of the first constructor
/**
******************
* Construct using a system defined string.
*
* @param paraString The given string.Its length should not exceed MAX_LENGTH-1.
******************
*/
public MyString(String paraString) {
data = new char[MAX_LENGTH];
length = paraString.length();
// Copy data
for (int i = 0; i < length; i++) {
data[i] = paraString.charAt(i);// charAt(i):返回i位置上的字符
} // Of for i
}// Of the second constructor
/**
******************
* Overrides the method claimed in Object,the superclass of any class.
******************
*/
public String toString() {
String resultString = "";
for (int i = 0; i < length; i++) {
resultString += data[i];
} // Of for i
return resultString;
}// Of toString
/**
******************
* Locate the position of a substring.
*
* @param paraString The given substring.
* @return The first position.-1 for no matching
******************
*/
public int locate(MyString paraMyString) {
boolean tempMatch = false;
for (int i = 0; i < length - paraMyString.length + 1; i++) {// 原字符串
// Initialize.
tempMatch = true;
for (int j = 0; j < paraMyString.length; j++) {// 带定位字符串移动
if (data[i + j] != paraMyString.data[j]) {
tempMatch = false;
break;
} // Of if
} // Of for j
if (tempMatch) {
return i;
} // Of if
} // Of for i
return -1;
}// Of locate
/**
******************
* Get a substring
*
* @param paraString The given substring.
* @param paraStartPosition The start position in the original string.
* @param paraLength The length of the new string.
* @return The first position. -1 for no matching.
******************
*/
public MyString substring(int paraStarPosition, int paraLength) {
if (paraStarPosition + paraLength > length) {
System.out.println("The bound id exceeded.");
return null;
} // Of if
MyString resultMyString = new MyString();
resultMyString.length = paraLength;
for (int i = 0; i < paraLength; i++) {
resultMyString.data[i] = data[paraStarPosition + i];
} // Of for i
return resultMyString;
}// Of substring
/**
******************
* The entrance of the program.
*
* @param args Not used now.
******************
*/
public static void main(String args[]) {
MyString tempFirstString = new MyString("I like ik.");
MyString tempSecondString = new MyString("ik");
int tempPosition = tempFirstString.locate(tempSecondString);
System.out.println(
"The position of \"" + tempSecondString + "\" in \"" + tempFirstString + "\" is: " + tempPosition);
MyString tempThirdString = new MyString("ki");
tempPosition = tempFirstString.locate(tempThirdString);
System.out.println(
"The position of \"" + tempThirdString + "\" in \"" + tempFirstString + "\" is: " + tempPosition);
tempThirdString = tempFirstString.substring(1, 2);
System.out.println("The substring is: \"" + tempThirdString + "\"");
tempThirdString = tempFirstString.substring(5, 5);
System.out.println("The substring is: \"" + tempThirdString + "\"");
tempThirdString = tempFirstString.substring(5, 6);
System.out.println("The substring is: \"" + tempThirdString + "\"");
}// Of main
}// Of class MyString
2.运行结果:
3.注意:
a.输出双引号,单引号的方法:
b.String类里的charAt(i)方法:用于返回指定索引处的字符。索引范围为从 0 到 length() - 1