串的顺序存储结构-截取子字符串
//获取子字符数组
private char[] subChar(char[] chars, int index, int length) {

    if (chars == null) {
        Console.WriteLine("截取的字符数组为空!");
        return null;
    }

    int cLength = chars.Length;
    if (length <1 || index < 1||index > cLength || index + length - 1 > cLength) {
        Console.WriteLine("索引和长度错误!");
        return null;
    }

    char[] newChar = new char[length];
    for (int i = 0; i < length; i++) {
        newChar[i] = chars[index - 1 + i];
    }

    return newChar;
}


//截取指定位置长度的串,成为一个新的串
public StringList SubString(int index, int length) {
    char[] sub = this.subChar(this._data, index, length);
    if (sub == null) {
        return null;
    }

    StringList newString = new StringList(sub);
    return newString;
}

首页 我的博客
粤ICP备17103704号