您的位置:首页 > 编程语言 > Java开发

Java学习笔记19(String类)

2018-01-10 16:08 477 查看
String代表字符串,在Java中,所有的字符串字面值都作为此类的实例实现

字符串的特点以及简单的原理分析:

package demo;
/*
* String类的特点:
* 所有的""都是String的对象
* 字符串一旦创建就是常量,不能改变
*/
public class StringDemo {
public static void main(String[] args) {
//发现创建对象不需要new
String str = "abcd";
System.out.println(str);
//输出:abcd而不是内存地址,因为String类重写了toString方法
str = "efgh";
System.out.println(str);
//输出:efgh,之前不是说常量不能改变吗?这里的原因:
//字符串本身是一个对象,在堆内存中,字符串本质是一个字符的数组
//源码:private final char value[];有final修饰,因此String是常量
//str = "efgh"是在内存中重新开一片,str的指向改变了,而"abcd"字符串对象没有改变

}
}


String类的创建方式和比较:

package demo;

public class StringDemo {
public static void main(String[] args) {
//字符串定义的两种方式,直接=,使用String的构造方法
String str1 = new String("abc");
String str2 = "abc";
//直接=更为方便,但是存在区别

System.out.println(str1==str2);//false
System.out.println(str1.equals(str2));//true

//使用构造方法定义原理:
//实际上创建了两个对象,一个是new String对象,一个是"abc"对象
//而new String对象中保存了"abc"这个字符数组对象的地址
//str1指向new String对象
//打印str1的时候,调用重写的toString方法找到字符串

//由于"abc"对象已经存在
//所以str2直接指向"abc"对象,不创建新的对象
//因此,str1==str2为false,因为对象的地址不同
//而str1.equals(str2)是true,原理:
//String类继承Object,重写了父类方法equals,建立了字符串自己的字符串比较方式
//即只要字符串的每个字符相同就是true,与地址无关
}
}


String类的构造方法:

1.官方资料:String(byte[] byte) 通过使用平台的默认字符集解码指定的byte数组,构造一个新的String

示例:

package demo;
/*
* String类的构造方法
*/
public class StringDemo2 {
public static void main(String[] args) {
byte[] bytes = {97,98,99,100};
String s = new String(bytes);
System.out.println(s);
//输出:abcd
//将字节数组中的每个字节,查询编码表得到的结果
}
}


2.官方资料:String(byte[] byte, int offset, int length)

示例:

package demo;
/*
* String类的构造方法
*/
public class StringDemo2 {
public static void main(String[] args) {
byte[] bytes = {65,66,67,68,69};
String s = new String(bytes, 2 ,3);
//第二个参数是起始索引,第三个参数是选取个数
System.out.println(s);
//输出:CDE
}
}


3.官方资料:String (char[] value) 分配一个新的String,使其表示字符数组参数中当前包含的字符序列

示例:

package demo;
/*
* String类的构造方法
*/
public class StringDemo2 {
public static void main(String[] args) {
function();
}
public static void function(){
char[] ch = {'a','b','c','d','e'};
String s = new String(ch);
System.out.println(s);
}
}
//输出:abcde


4.和前两个相似,官方资料:String(char[] value, int offset, int length)

示例:

package demo;
/*
* String类的构造方法
*/
public class StringDemo2 {
public static void main(String[] args) {
function();
}
public static void function(){
char[] ch = {'a','b','c','d','e'};
String s = new String(ch,1,3);
System.out.println(s);
}
}
//输出:bcd


还有很多的构造方法,不过常用的是这些

String类的其他方法:

1.官方资料:int length() 返回此字符串的长度(比较简单,不做示例了)

2.官方资料:String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串

另一个:String substring(int beginIndex,int endIndex) 同上,多了一个结束索引值

示例:

package demo;

public class StringDemo2 {
public static void main(String[] args) {
function();
}
public static void function(){
String str = "iamhandsome";
String str1 = str.substring(3);
String str2 = str.substring(3, 5);
System.out.println(str1);
System.out.println(str2);
}
}
/*输出:
handsome
ha

这里注意,区间是左闭右开,意思是包括起始索引,不包括终止索引
*/


3.官方资料:boolean startsWith(String prefix)

      测试字符串是否以指定的前缀开始

      有前缀也应该有后缀:

     boolean endWith(String suffix)

4.官方资料:boolean contains(String s)判断一个字符串中是否包含另一个字符串

5.int indexOf(char ch) 查找一个字符在字符串中第一次出现的索引

3,4,5示例:

package demo;

public class StringDemo2 {
public static void main(String[] args) {
function();
}

public static void function() {
String string = "Iamhandsome";
boolean b = string.startsWith("Ia");
System.out.println(b);// true
boolean b1 = string.endsWith("ome");
System.out.println(b1);// true
boolean b2 = string.contains("hand");
System.out.println(b2);//true
int a = string.indexOf("a");
System.out.println(a);//1
}
}


6.将字符串转成字节数组:

  byte[] getBytes()

  此功能和String的构造方法相反

示例:

package demo;

public class StringDemo2 {
public static void main(String[] args) {
function();
}

public static void function() {
String str = "abc";
byte[] bytes = str.getBytes();
for(int i=0; i<bytes.length; i++){
System.out.println(bytes[i]);
}
}
}
/*输出:
97
98
99
*/


7.将字符串转成字符数组:char[] toCharArray();

package demo;

public class StringDemo2 {
public static void main(String[] args) {
function();
}

public static void function() {
String str = "abc";
char[] chars = str.toCharArray();
for(int i=0; i<chars.length; i++){
System.out.println(chars[i]);
}
}
}
/*输出:
a
b
c
*/


比较字符串是否相同

示例:

package demo;

public class StringDemo2 {
public static void main(String[] args) {
function();
}

public static void function() {
String str1 = "Abc";
String str2 = "abc";
boolean b1=str1.equals(str2);
boolean b2 = str1.equalsIgnoreCase(str2);
System.out.println(b1);//false
System.out.println(b2);//true
//比较字符串是否相同,第二个忽略大小写
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: