作者: 暮想sun | 来源:发表于2019-12-22 16:02 被阅读0次

    串是由零个或多个字符组成的有限序列,又名叫字符串。

    初始化构造:

        //字符串存储数据
        private char[] values;
    
        public TestString() {
        }
    
        public TestString(char[] values) {
            this.values = values;
        }
    
        public TestString(char[] value, int offset, int count) {
            if (offset < 0) {
                throw new StringIndexOutOfBoundsException(offset);
            }
            if (count <= 0) {
                if (count < 0) {
                    throw new StringIndexOutOfBoundsException(count);
                }
                if (offset <= value.length) {
                    this.values = new TestString().values;
                }
            }
            // Note: offset or count might be near -1>>>1.
            if (offset > value.length - count) {
                throw new StringIndexOutOfBoundsException(offset + count);
            }
    
            //copy为新数组数据
            char[] copy = new char[count];
            System.arraycopy(value, offset, copy, 0, count);
            this.values = copy;
        }
    
    

    字符串长度:

        public int length() {
            return values.length;
        }
    

    字符串是否为空:

        public Boolean isEmpty() {
            return values.length == 0;
        }
    
    

    添加字符串:

        public TestString concat(TestString str) {
            int addStrLength = str.length();
            if (addStrLength == 0) {
                return this;
            }
    
            //新建数组,长度为字符串原长度+新加入字符串长度之和
            int srcStrLength = values.length;
            char[] newCharArr = Arrays.copyOf(values, addStrLength + srcStrLength);
    
            //将新加入字符串加入新数组中
            System.arraycopy(str.values, 0, newCharArr, srcStrLength, addStrLength);
            return new TestString(newCharArr);
        }
    

    截取字符串:

        public TestString subString(int start, int end) {
    
            if (start < 0) {
                throw new StringIndexOutOfBoundsException(start);
            }
            if (end > values.length) {
                throw new StringIndexOutOfBoundsException(end);
            }
    
            int subLen = end - start;
            if (subLen < 0) {
                throw new StringIndexOutOfBoundsException(subLen);
            }
    
            return new TestString(values, start, end);
    
        }
    

    字符串匹配:

        public int indexOf(TestString str) {
            //目标数据从第一个开始比对
            char first = str.values[0];
            //剩余最大长度,从0开始比较到max时,没有匹配到数据,就不用匹配了,后边数据长度不够
            int max = values.length - str.values.length;
    
            //for循环的含义为,继续寻找下一个匹配第一个字符的下标
            for (int i = 0; i <= max; i++) {
                //
                if (values[i] != first) {
                    while (++i <= max && values[i] != first) {
    
                    }
                }
    
                //碰到数据与first相等,此时下标为i
                if (i <= max) {
                    //继续匹配i下一个元素与target元素匹配
                    int j = i + 1;
                    int end = j + str.values.length - 1;
                    for (int k = 1; j < end && values[j]
                        == str.values[k]; j++, k++) {
    
                    }
    
                    //如果顺利匹配到最后一位且成功,则匹配成功
                    if (j == end) {
                        return i;
                    }
                }
            }
            return -1;
        }
    
    

    相关文章

      网友评论

        本文标题:

        本文链接:https://www.haomeiwen.com/subject/zfkonctx.html