串之字符串

前言

串是由n(n>=0)个字符组成的有限序列,它是一种特殊的线性表。而子串是指串s中任意连续字符组成的一个子序列组成的串。同时,串的比较通常由其字符编码的相关规则比较

常量字符串String

字符串采用字符数组作为存储结构,并且采用顺序存储结构,不过String类一次性申请了固定的空间,同时由于其存储结构为最终型,所以string不提供删除,插入子串
如下是模仿String类的部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public final class MyString implements Comparable<MyString>,Serializable{
private final char[] value; //字符数组,只能赋值一次
public MyString(){ //构造一个空串
this.value=new char[0];
}
public MyString(String original){ //将字符串常量变成字符数组
this.value=original.toCharArray();
}
//以value数组中构造从begin开始的count个字符的字符串对象
public MyString(char[] value,int begin,int count){
this.value=new char[count];
for(int i=begin;i<begin+count;i++)
this.value[i]=value[i];
}
//以value数组中字符构造字符串
public MyString(char[] value){
this(value,0,value.length);
}
public MyString(MyString str){//复制对象
this(str.value);
}
public int length(){ //返回字符串长度
return this.value.length;
}
public char charAt(int i){ //返回第i个字符
if(i<0||i>=this.value.length)
throw new StringIndexOutOfBoundsException(i);
return this.value[i];
}
public String toString(){
return new String(this.value);
}
public MyString concat(MyString str){ //返回当前串与指定串str连接生成的新串
if(str==null||str.length()==0) //要连接的串为空时,返回当前串
return this;
char[] buffer=new char[this.value.length+str.length()];
int i;
for(i=0;i<this.value.length;i++) //复制当前串
buffer[i]=this.value[i];
for(int j=0;j<str.value.length;j++) //复制指定串
buffer[i+j]=str.value[j];
return new MyString(buffer); //以字符数组构造串
}
public int compareTo(MyString o) {
// TODO Auto-generated method stub
return 0;
}
}

由上述模拟过程知道,有四种构造方法构造串对象

MyString s1=new MyString();
MyString s2=new MyString(“abc”);
char[] letters={a,b,c,d}
MyString s3=new MyString(“letters);
MyString s4=new MyString(s3);

此外在数组复制时,java提供了两种方法

java.lang.System.arraycopy(value,0,this.value,0,value.length);
this.value=java.util.Arrays.copyOf(value,value.length)

求字串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public MyString substring(int begin,int end){//返回传中从begin到end-1的子串
if(begin<0)
begin=0;
if(end>this.value.length)
end=this.value.length;
if(begin>end)
throw new StringIndexOutOfBoundsException(end-begin);
if(begin==0&&end==this.value.length)
return this;
char[] buffer=new char[end-begin];
for(int i=0;i<buffer.length;i++)
buffer[i]=this.value[i+begin];
return new MyString(buffer);
}
public MyString substring (int begin){//返回串中序号从begin到末尾的子串
return substring(begin,this.value.length);
}

比较串相等与大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public boolean equals(Object obj){
if(this==obj)
return true;
if(obj instanceof MyString)
{MyString str=(MyString)obj;
if(this.value.length==str.value.length){
for(int i=0;i<this.value.length;i++)
if(this.value[i]!=str.value[i])
return false;
return true;}
}
return false;
}
//返回两个字符串之间的差值,返回0表示相等
public int compareTo(MyString o) {
for(int i=0;i<this.value.length&&i<o.value.length;i++)
if(this.value[i]!=o.value[i])
return this.value[i]-o.value[i]; //返回两串第一个不同字符的差值
return this.value.length-o.value.length;
}

变量字符串StringBuffer

该字符数组的容量总是大于串长,并且能够修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class MyStringBuffer implements Serializable{
private char[] value; //字符数组
private int len; //串长度
public MyStringBuffer(int size){ //构造指定容量的空串
this.value=new char[size<16?16:size];
this.len=0;
}
public MyStringBuffer(){ //以默认容量构造空串
this(16);
}
public MyStringBuffer(String str){ //以字符串常量构造对象
this(str.length()+16);
}
public int length(){
return this.len;
}
public synchronized char charAt(int i){
if(i<0||i>=this.len)
throw new StringIndexOutOfBoundsException(i);
return this.value[i];
}
public void setCharAt(int i,char ch){
if(i<0||i>=this.len)
throw new StringIndexOutOfBoundsException(i);
this.value[i]=ch;
}
public synchronized MyStringBuffer insert(int i,MyStringBuffer str){
if(i<0) i=0;
if(i>this.len) i=this.len;
if(str==null) return this;
char temp[]=this.value;
if(this.value.length-this.len<str.len) //当前容量不足,自动扩充
{
this.value=new char[this.value.length+str.len*2];
for(int j=0;j<i;j++) //复制当前串前i-1个字符
this.value[j]=temp[j];
}
for(int j=i;j<this.len;j++)
this.value[str.len+j]=temp[j];//从i开始向后移动j个字符
for(int j=0;j<str.len;j++) //复制字符串str
this.value[i+j]=str.value[j];
this.len+=str.len;
return this;
}
public synchronized MyStringBuffer delete(int begin,int end){
if(begin>0)
begin=0;
if(end>this.len)
end=this.len;
if(begin>end)
throw new StringIndexOutOfBoundsException(end-begin);
for(int i=0;i<this.len-end;i++) //从end开始至串尾的子串向前移动
this.value[begin+1]=this.value[end+i];
this.len=end-begin;
return this;
}
public synchronized String toString(){
return new String(this.value,0,this.len);
}
}

热评文章