1 package com.tmx.string; 2 3 public class MyString implements java.io.Serializable { 4 private static final long serialVersionUID = -1597485086838057214L; 5 /** 6 * 字符数组,私有最终变量,只能赋值一次 7 */ 8 private final char[] value; 9 10 /** 11 * 构造空串"",串长度为0 12 */ 13 public MyString() { 14 this.value = new char[0]; 15 } 16 17 /** 18 * 由字符串常量str构造串,复制拷贝的过程 19 * @param str 20 */ 21 public MyString(java.lang.String str) { 22 this.value = new char[str.length()]; 23 for (int i = 0; i < this.value.length; i++) { 24 this.value[i] = str.charAt(i); 25 } 26 } 27 28 /** 29 * 以字符数组value的i开始的count个字符构造串,i>=0,count>=0,i+count<=value.length 30 * @param value 31 * @param i 32 * @param count 33 */ 34 public MyString(char[] value, int i, int count) { 35 if (i >= 0 && count >= 0 && i + count <= value.length) { 36 this.value = new char[count]; 37 for (int j = 0; j < count; j++) { 38 this.value[j] = value[i + j]; 39 } 40 } else { 41 throw new StringIndexOutOfBoundsException("i=" + i + ",count=" + count + ",i+count=" + (i + count)); 42 } 43 } 44 45 /** 46 * 以整个字符数组构造串 47 * @param value 48 */ 49 public MyString(char[] value) { 50 this(value, 0, value.length); 51 } 52 53 /** 54 * 以byte数组bytes的一部分构造串 55 * @param bytes 56 * @param begin 57 * @param length 58 */ 59 public MyString(byte[] bytes, int begin, int length) { 60 if (begin >= 0 && length >= 0 && begin + length <= bytes.length && begin <= length) { 61 this.value = new char[length]; 62 for (int i = begin; i < length; i++) { 63 this.value[i] = (char) (bytes[i]); 64 } 65 } else { 66 throw new StringIndexOutOfBoundsException( "begin=" + begin + ",length=" + length + ",begin+length=" + (begin + length)); 67 } 68 } 69 70 /** 71 * 重载构造方法 72 * @param bytes 73 */ 74 public MyString(byte[] bytes) { 75 this(bytes, 0, bytes.length); 76 } 77 78 /** 79 * 深度拷贝,复制数组 80 * @param str 81 */ 82 public MyString(MyString str) { 83 this(str.value); 84 } 85 86 /** 87 * 串长度即为字符数组的长度 88 * @return 89 */ 90 public int length() { 91 return this.value.length; 92 } 93 94 public java.lang.String toString() { 95 return new String(this.value); 96 } 97 98 /** 99 * 返回第index个字符,0<=index<length(); 100 * @param index 101 * @return 102 */ 103 public char charAt(int index) { 104 if (index >= 0 && index < this.length()) { 105 char cha = this.value[index]; 106 return cha; 107 } else { 108 throw new StringIndexOutOfBoundsException("index=" + index); 109 } 110 } 111 112 /** 113 * 判断两个串是否相等 114 * @param str 115 * @return 116 */ 117 public boolean equals(java.lang.String str) { 118 boolean flag = false; 119 if (this.value.length != str.length()) { 120 flag = false; 121 } else { 122 for (int i = 0; i < str.length(); i++) { 123 if (this.value[i] != str.charAt(i)) { 124 flag = false; 125 break; 126 } else { 127 flag = true; 128 } 129 } 130 } 131 return flag; 132 } 133 134 /** 135 * 返回序号从begin--(end-1)的子串 136 * @param begin 137 * @param end 138 * @return 139 */ 140 public MyString substring(int begin, int end) { 141 if (begin >= 0 && end <= this.length() && begin <= end) { 142 if (begin == 0 && end == this.length()) { 143 return this; 144 } else { 145 return new MyString(this.value, begin, end - begin); 146 } 147 } else { 148 throw new StringIndexOutOfBoundsException("begin=" + begin + ",end=" + end); 149 } 150 } 151 152 /** 153 * 重载 154 * @param begin 155 * @return 156 */ 157 public MyString substring(int begin) { 158 return this.substring(begin, this.length()); 159 } 160 161 /** 162 * 从指定位置开始查找指定的字符的位置,没有返回-1 163 * @param cha 164 * @param begin 165 * @return 166 */ 167 public int indexOf(char cha, int begin) { 168 int j = 0; 169 for (int i = begin; i < this.value.length; i++) { 170 if (cha == this.value[i]) { 171 j = i; 172 break; 173 } else { 174 j = (-1); 175 } 176 } 177 return j; 178 } 179 180 /** 181 * 重载indexOf方法,从头开始查找 182 * @param cha 183 * @return 184 */ 185 public int indexOf(char cha) { 186 return this.indexOf(cha, 0); 187 } 188 189 /** 190 * 查找指定的字符串的位置,未找到返回-1 191 * @param str 192 * @return 193 */ 194 public int indexOf(java.lang.String str) { 195 return this.indexOf(str, 0); 196 } 197 198 /** 199 * 从指定的位置开始查找指定的串位置 200 * @param str 201 * @param begin 202 * @return 203 */ 204 public int indexOf(java.lang.String str, int begin) { 205 int j = 0; 206 int strLength = str.length(); 207 if (begin < 0) { 208 begin = 0; 209 } 210 if (strLength > this.length() || strLength + begin > this.length() || begin > this.length()) { 211 j = (-1); 212 } else { 213 int beginInt = this.indexOf(str.charAt(0), begin); 214 if (beginInt == (-1)) { 215 j = (-1); 216 } else { 217 MyString str1 = this.substring(beginInt, beginInt + strLength); 218 for (int i = 0; i < str1.length(); i++) { 219 if (str1.charAt(i) == str.charAt(i)) { 220 j = beginInt; 221 } else { 222 j = (-1); 223 } 224 } 225 } 226 } 227 return j; 228 } 229 }
时间: 2024-10-12 03:56:10