String类特点分析
目录索引
String类简介
字符串严格意义上来讲,并不能算是一个基本数据类型,只是在Java里面,为了方便开发者进项项目的编写,所以利用其JVM的支持制造了一种可以简单使用的String类,其可以保存字符串的原因主要在于,其每一个字符的数据都是保存在了数组之中。String本身包装的是一个数组,并且其有两种对象的实例化形式:直接赋值、构造方法赋值。
字符串比较
String类的相等判断也可以使用“==”来比较,但不准确,所以可以使用String类提供的方法String A.equals(String B); 来实现。
关于对象相等判断的小技巧:
如果要求将用户输入数据(String)和一个字符串进行比较,使用equals()时,可以将变量写在前面,比如:
public class StringDemo {
public static void main(String args[]) {
String input = null; // 如果用户输入的内容为null。
System.out.println(input.equals("mldn"));//接受用户输入数据后调用equals()然后会出现NullPointerException错误提示
System.out.println("mldn".equals(input);//将字符串常量写在前面,就可以避免为null的判断异常
}
}
String类对象两种实例化方式比较
- 直接赋值的处理操作: 对于字符串而言可以实现池数据的自动保存,这样如再有相同的数据定义时,可以减少对象的产生实现重用,以提升操作性能
比如:
public class StringDome {
public static void main(String args[]){
String strA = "mldn";
String strB = "mldnjava";
String strC = "mldn";
System.out.println(strA==strB);//地址判断
}
}
- 构造方法实例化:
- 此时会开辟两块堆内存空间,而后只会使用一块,而另一块由于字符串常量所定义的匿名对象将会成为垃圾空间:
public class StringDemo {
public static void main(String args[]) {
String str = new String("mldn") ;
}
}
2. 不会出现自动保存到字符串池的处理,无法实现对像的重用,实际上是属于一种自己专用的内存空间,但是在String 类里面也提供了又帮助开发者 实现手工入池的处理情况,方法:public String intern();
public class StringDemo {
public static void main(String args[]) {
String strA = "mldn" ;
String strB = new String("mldn") ;
/*String StrB = new String("mldn").intern();//手动入池
System.out.println(strA==strB)//此时会返回true*/
}
}
String对象(常量)池
对象池的主要目的是实现数据的共享。但是在Java中对象(常量)池实际上可以分为两种;
- 静态常量池:程序(*.class)文件在加载的时候回自动将此程序质中保存的字符串、普通的常量、类已将方法等,全部进行分配;
public class StringDemo {
public static void main (String args[]) {
String strA = "www.mldn. cn" ;
String strB = "www." + "mldn" + " .cn" ;
System. out. println(strA == strB) ; //返回true
}
}
- 动态常量池: 当一个程序(*.class)加载后,里面可能有一些变量,这个时候提供的一个常量池;
public class StringDemo {
public static void main (String args[]) {
String info="mldn" ;
String strA = "www.mldn.cn";
String strB = "www." + info +".cn";
System.out. println(strA== strB); // 返回false.
}
}
字符串内容不可修改
public class StringDemo {
public static void main (String args[]) {
String s="a"+"b"+"c"//这个不叫修改
String str = "www." ;
str += "mldn." ;
str=str+"cn";//这个才叫修改
System. out. println(str) ;
}
}
内存分析:String常量的内容没有改变,改变的只是引用,并带来了大量的垃圾空间
主方法的组成
public static void main(String args[])
- public: 描述的是一种访问权限,主方法是一切的开始点,开始点一定是公共的!
- static: 程序的执行是通过类名称完成的,所以表示此方法是由类直接调用;
- void: 主方法是一切的起点,起点一旦开始就没有返回的可能;
- main: 是一个系统定义好的方法名称;
- String args[]: 字符串的数组,可以实现程序启动参数的接收。
public class StringDemo {
public static void main (String args[]) {
for (String arg : args) {
System.out.println(arg) ;
}
}
String常用方法
可以查看JavaDoc文档以及里面的String类官方API文档
字符串与字符数组、字节数组
字符串与字节的转换主要是为了进行二进制的数据传输,或者是进行编码转换
方法名称 | 类型 | 说明 |
---|---|---|
public String(char[] value) | 构造 | 将传入的全部字符数组变为字符串 |
public String(char[] value, int offset, int count) | 构造 | 将部分字符数组变为字符串 |
public char charAt(int index) | 普通 | 获取指定索引位置的字符 |
public char[] toCharArray() | 普通 | 将字符串中的数据以字符数组的形式返回 |
public String(byte[] bytes) | 构造 | 将全部字节数组 变为字符串 |
public String(byte[] bytes, int offset, int length) | 构造 | 将部分字节数组变为字符串 |
public byte[] getBytes() | 普通 | 将字符串转换为字节数组 |
public byte[] getBytes(String charsetName) throwsUnsupportedEncodingException | 普通 | 编码转换 |
字符串比较、查找、替换、拆分以及截取
比较
方法名称 | 类型 | 说明 |
---|---|---|
public boolean equals(String anObject) | 普通 | 区分大小写相等判断 |
public boolean equalsIgnoreCase( String anotherString) | 普通 | 不区分大小写相等判断 |
public int compareTo(String anotherString) | 普通 | 进行字符串大小比较,返回差值 |
public int compareToIgnoreCase(String str) | 普通 | 不区分大小写字符串比较 |
查找
方法名称 | 类型 | 说明 |
---|---|---|
public public boolean contains(String s) | 普通 | 判断子字符串是否存在,存在返回true |
public int indexOf(String str) | 普通 | 从头查找指定字符串的位置,找到就返回找到的位置找不到返回-1 |
public int indexOf(String str, int fromIndex) | 普通 | 从指定位置查找指定字符串的位置 |
public int lastIndexOf(String str) | 普通 | 由后向前查找指定字符串的位置 |
public int lastIndexOf( String str, int fromIndex) | 普通 | 从指定位置由后向前查找指定字符串的位置 |
public boolean startsWith(String prefix) | 普通 | 判断是否以指定的字符串开头 |
public boolean startsWith(String prefix, int toffset) | 普通 | 由指定位置判断是否是指定的内容 |
public boolean endsWith(String sufix) | 普通 | 是否已指定的字符串结尾 |
替换
方法名称 | 类型 | 说明 |
---|---|---|
public String replaceAll(String regex, String replacement) | 普通 | 全部替换 |
public String replaceFirst(String regex, String replacement) | 普通 | 替换首个 |
拆分
会遇见拆不了的情况,这个时候在指定字符串前使用“\”进行转义
方法名称 | 类型 | 说明 |
---|---|---|
public String[] split (String regex) | 普通 | 按照指定字符串全部拆分 |
public String[] split(String regex, int limit) | 普通 | 按照指定字符串拆分为指定个数,后面的不拆了 |
截取
方法名称 | 类型 | 说明 |
---|---|---|
public String substring(int beginIndex) | 普通 | 从指定索引截取到结束 |
public String substring(int beginIndex,int endIndex) | 普通 | 截取指定索引范围的子字符串 |
字符串格式化以及其他操作方法
格式化
方法名称 | 类型 | 说明 |
---|---|---|
public static String format(String format,各种类型 … args) | 普通 | 根据指定结构进行文本格式化 |
public class StringDemo {
public static void main (String args[]) {
String name = "张三" ;
int age=18;
double score = 98.765321 ;
String str = String.format("姓名: %s、 年龄: %d、 成绩: %5.2f",name,age,score);
System.out.println(str) ;//打印结果-姓名:张三、年龄: 18、成绩: 98.77。
}
}
其他操作方法
方法名称 | 类型 | 说明 |
---|---|---|
public String concat(String str) | 普通 | 字符串的连接 |
public boolean isEmpty() | 普通 | 判断是否为空(不是判断nll) |
public String trim() | 普通 | 去除左右的空格信息 |
public String toUpperCase() | 普通 | 转大写 |
public String toLowerCase() | 普通 | 转小写 |