java的String功能特点
Sring字符串是一个类,属于引用数据类型,提供比较大小、连接串等方法
String的对象是不是一个字符数组,不能以数组下标格式s[i]进行操作,这和c/c++不一样!!
String是java一个特殊类,不仅约定了其常量形式,还重载“=”,使它能和基本数据类型一样,进行赋值和运算 例如:String str = "java"。
java字符串常量和c/c++形式一样,用双引号和括起来 例如:“java”。
java字符串 采用单引号,只能是char类型。例如'a'。
java中的String类特点
String是最终类,不能被继承
String类以字符串方式存储和实现字符串操作,采用字符串数组存储字符序列
但是,java数组容量就等于串长度,串尾没有'\0'作为字符串结束符,这和c/c++不一样!!!
见下图

声明字符数组是最终变量,串中各种字符是只读的。当构造串对象时,对字符串数组进行一次赋值,不能更改,String只提供了字符操作charAr(i),不提供修改字符、插入字符等操作。
构造串、求子串和连接串的操作都是深拷贝,重新申请字符串占用的字符数组,复制字符数组,不会改变原串。
代码演示:
为防止与String类名字相同,导致冲突,下面的类名为MyString
MyString类
/*
实现以下功能
四种构造方法:无参构造、字符串常量构造、字符串数组构造、MyString对象构造
实现方法:getLenth()、charAt()、subString()、contact、
*/
public final class MyString {
private final char[] value; //字符数组,私有最终变量,只能赋值一次,且不能修改
//默认构造
public MyString() {
this.value = new char[0];
}
//字符串常量构造
public MyString(String s) {
this.value = new char[s.length()];
for (int i = 0; i < this.value.length; i++) {
value[i] = s.charAt(i);
}
}
//字符数组进行构造
//以str数组,从索引第i个位置,进行复制n个字符串
//i>=0 , n>=0 , i+n<=str.lenth
public MyString(char[] str, int i, int n) {
if (i >= 0 && n >= 0 && (i + n) <= str.length) {
this.value = new char[n];
System.arraycopy(str,i,this.value,0,n - i);
//这种也可也
// for (int j = i; j < n; j++) {
// this.value[j] = str[j];
// }
} else {
throw new StringIndexOutOfBoundsException("i=" + i + ", n=" + n + " i+n=" + (i + n));
}
}
// 字符数组进行构造
public MyString(char[] str) {this(str, 0, str.length);
}
//MyString构造
public MyString(MyString s) {this(s.value);}
@Override
public String toString() {
String str = this.getClass().getName()+"(";
for (int i = 0; i < this.value.length; i++) {
str += this.value[i];
}
return str+")";
}
/*
public int getLenth()
返回字符串长度
retturn:返回字符串长度
*/
public int getLenth() {return this.value.length;}
/*
public char charAt(int i)
返回索引i对应的元素
return:value[i]
*/
public char charAt(int i) {
//i必须在合法范围内
if (i >= 0 && i <= this.value.length) {
return this.value[i];
} else {
//否则就抛出异常
throw new StringIndexOutOfBoundsException("i=" + i);
}
}
/*
返回数组类型的子串
注意:截取范围为左开右闭
public char[] subCharString(int begin, int end)
begin:开始截取的元素索引
end:结束截取的元素索引,注意,到end为结束,实际截取的是end - 1
return:截取后的数组
*/
public char[] subCharString(int begin, int end) {
//方法1
//将原串中截取一部分作为子串
if (begin >= 0
&& begin <= end && end <= this.value.length) {
//构建临时数组,存储子串
char []temp = new char[end - begin];
for (int i = begin; i < end; i++) {
temp[i] = this.value[i];
}
return temp;
}
return null;
}//subString
/*
返回MyString类型的子串
注意:截取范围为左开右闭
public MyString subMyString(int begin, int end)
begin:开始截取的元素索引
end:结束截取的元素索引,注意,到end为结束,实际截取的是end - 1
return:截取后的新对象
*/
public MyString subMyString(int begin, int end){
//如果截取范围和数组范围一样
if(begin == 0 && end == this.value.length){
return this;
}
return new MyString(this.value, begin,end - begin);
}
public MyString subMyString(int begin){
return subMyString(begin,this.value.length);
}
/*
public MyString contact(MyString s)
连接两个字符串
s:要连接的MyString对象
return:连接好的MyString对象
*/
public MyString contact(MyString s){
//连接this.value 和 s
//如果为空,就复制现在存在value
if (s == null || s.equals("")){ s = new MyString(this.value);}
int len = this.value.length + s.getLenth();//连接后的数组长度
char []buffer = new char[len];
int j;
for ( j = 0; j < this.value.length; j++) {buffer[j] = this.value[j];}
for (int i = 0 ; i < s.value.length; i++) {buffer[i + j] = s.value[i];}
//下面的for循环是测试是否数据已经存进了buffer数组
/*
for (int i = 0; i < buffer.length; i++) {
System.out.print("buffer"+i+buffer[i]+",");
}
System.out.println("");
System.out.println("buffer:"+buffer[0]);
*/
return new MyString(buffer);
}
/*
判断字符串是否以指定前缀开始
*/
public boolean startWtih(String prefix){
//java API文档规定如果perfix为空,也返回true
//如果prefix与this一样,也返回trie
if(prefix == null || prefix == ""
|| this.value.equals(prefix)){
return true;
}
if(prefix.charAt(0) == this.value[0]){
return true;
}
return false;
}
/*
public boolean endWith(String suffix)
是否以指定的字符串缀结束
*/
public boolean endWith(String suffix){
//字符串为空或与this相同,返回true
if(suffix == null || suffix == "" || this.equals(this.value) ){ return true;}
if(suffix.charAt(suffix.length() - 1) == this.value[this.value.length - 1] ){return true;}
return false;
}
}Test测试类
public class Test {
public static void main(String[] args) {
//创建对象
MyString myString = new MyString("abcd");
//测试getLenth()方法
System.out.println(myString.getLenth());
// 测试charAt()方法
System.out.println(myString.charAt(0));
//测试toString()方法
System.out.println(myString.toString());
//测试subCharString()方法
System.out.println(myString.subCharString(0,3));
// 测试contact()方法
MyString myString1 = new MyString("123");
MyString myString2 = myString.contact(myString1);
System.out.println(myString2.toString());
//测试startWtih()方法
System.out.println(myString.startWtih(""));
//测试endWith()方法
System.out.println(myString.endWith("-1"));
}
}
文章详细介绍了Java中String类的特点,包括其不可变性、存储方式以及与C/C++的区别。此外,文章还展示了如何自定义一个名为MyString的类,实现字符串的构造、获取长度、字符访问、子串提取和连接等功能。示例代码展示了MyString类的实现细节和测试用例。
7万+

被折叠的 条评论
为什么被折叠?



