Java之String类常用操作

Java之String类常用操作

Java之String类

一、String类的理解1、类的声明2、内部声明的属性3、字符串常量的存储位置4、字符串的不可变性的理解5、String实例化的两种方式6、字符串的拼接

二、String的构造器1、构造器2、String和char之间相互转换3、String和byte之间相互转换

三、String中常用方法

一、String类的理解

1、类的声明

public final class String

implements java.io.Serializable, Comparable, CharSequence {

final:String是不可以被继承的; Serializable:可序列化的接口,凡是实现此接口的类的对象就可以通过网络或者本地流进行数据的传输。 Comparable:凡是实现此接口的类,其对象都可以比较大小。

2、内部声明的属性

private final char value[];

存储字符串数据的容器

final:指明此value数组一旦初始化,其地址就不可变

3、字符串常量的存储位置

字符串常量都存储在字符串常量池(StringTable)中 字符串常量池不允许存放两个相同的字符串常量 字符串常量池在不同的jdk版本中,存放的位置不同

4、字符串的不可变性的理解

1、当对字符串变量重新赋值时,需要重新指定一个字符串常量的位置进行赋值,不能在原来的位置修改 2、对现有的字符串进行拼接操作时,需要重新开辟空间保存新的字符串。 3、当调用字符串的replace方法替换现有的某个字符时,需要重新开辟空间保存修改以后的字符串,不能原地修改

public class StringDemo {

public static void main(String[] args) {

StringDemo s = new StringDemo();

s.test2();

s.test3();

}

// todo String的不可变性

// 当对字符串变量重新赋值时,需要重新指定一个字符串常量的位置进行赋值,不能在原来的位置修改

// 对现有的字符串进行拼接操作时,需要重新开辟空间保存新的字符串。

// 当调用字符串的replace方法替换现有的某个字符时,需要重新开辟空间保存修改以后的字符串,不能原地修改

public void test2() {

String s1 = "hello";

String s2 = "hello";

s2 = "hi";

s2+="world";

System.out.println(s1); // todo hello

System.out.println(s1); // todo hello

}

public void test3() {

String s1 = "hello";

String s2 = "hello";

String s3=s2.replace('l','o');

System.out.println(s1); // hello

System.out.println(s2); // hello

System.out.println(s3); // heooo

}

}

5、String实例化的两种方式

String s1 = “hello”; String s2 = new String(“hello”);

public class StringDemo1 {

public static void main(String[] args) {

StringDemo1 s = new StringDemo1();

s.test1();

}

public void test1(){

String s1 = "hello";

String s2 = "hello";

String s3 = new String("hello");

String s4 = new String("hello");

System.out.println(s1==s2); //true

System.out.println(s1==s3); //false

System.out.println(s1==s4); //false

System.out.println(s3==s4); //false

System.out.println(s1.equals(s2)); //true

System.out.println(s1.equals(s3)); //true

System.out.println(s1.equals(s4)); //true

System.out.println(s3.equals(s2)); //true

}

}

6、字符串的拼接

1、常量+常量:结果仍然存储在字符串常量池;此时的常量可能是字面量,也可能是final修饰的变量。 2、常量+变量 或者 变量+常量:都会通过new的方式创建一个新的字符串,返回堆空间中此字符串对象的地址 3、调用字符串的intern():返回字面量的地址

public void test2() {

String s1 = "hello";

String s2 = "world";

String s3 = "helloworld";

String s4 = "hello" + "world";

String s5 = s1 + "world"; //todo 通过查看字节码文件发现调用了StringBuilder()——》new String()

String s6 = "hello" + s2;

String s7 = s1 + s2;

System.out.println("------------------------------");

System.out.println(s3 == s4); //true

System.out.println(s3 == s5); //false

System.out.println(s3 == s6); //false

System.out.println(s3 == s7); //false

System.out.println(s5 == s6); //false

System.out.println(s5 == s7); //false

}

public void test3() {

final String s1 = "hello";

final String s2 = "world";

String s3 = "helloworld";

String s4 = "hello" + "world";

String s5 = s1 + "world"; //todo 通过查看字节码文件发现调用了StringBuilder()——》new String()

String s6 = "hello" + s2;

String s7 = s1 + s2;

System.out.println("------------------------------");

System.out.println(s3 == s5); //true

System.out.println(s3 == s6); //true

}

二、String的构造器

1、构造器

public String() :初始化新创建的 String对象,以使其表示空字符序列。 public String(String original):初始化一个新创建的“String”对象,使其表示一个与参教相同的字符序列 public String(char[] value):通过当前参数中的字符数组来构造新的String。 public String(char[] valve,int offset,int count):通过字符数组的一部分来构造新的String。 public String(byte[] bytes):通过使用平台的默认字符集解码当前参数中的字节数组来构造新的String。 public String(byte[] bytes,String charsetName)':通过使用指定的字符集解码当前参数中的字节数组来构造新的String。

2、String和char之间相互转换

String——》char[]:调用String的toCharArray()方法 char——》String:调用String的构造器

public class StringMethodTest {

public static void main(String[] args) {

StringMethodTest s = new StringMethodTest();

s.test1();

s.test2();

s.test3();

}

public void test1() {

String s1 = new String();

String s2 = new String("");

String s3 = new String(new char[]{'a', 'b', 'c'});

System.out.println(s3);

}

public void test2() {

String str = "hello";

//todo String——》char[]:调用String的toCharArray()方法

char[] arr = str.toCharArray();

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);

}

//todo char——》String:调用String的构造器

String str1 = new String(arr);

System.out.println(str1); //hello

}

}

3、String和byte之间相互转换

String——》byte[]:调用String的getBytes()方法 getBytes(String charsetName):使用指定的字符集

在utf-8字符集中,一个汉字占用3个字节,一个字母占用1个字节。 在gbk字符集中,一个汉字占用2个字节,一个字母占用1个字节。

public class StringMethodTest {

public static void main(String[] args) {

StringMethodTest s = new StringMethodTest();

s.test1();

s.test2();

s.test3();

}

// String与byte[]之间的转换

// 在utf-8字符集中,一个汉字占用3个字节,一个字母占用1个字节。

// 在gbk字符集中,一个汉字占用2个字节,一个字母占用1个字节。

public void test3() {

String str = "中国";

//todo String——》byte[]:调用String的toCharArray()方法

byte[] arr = str.getBytes(); //使用默认的字符集

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]); // -28\-72\-83\-27\-101\-67

}

System.out.println();

// String str1 = new String("abc中国");

// // todo getBytes(String charsetName):使用指定的字符集

// byte[] arr1 = str1.getBytes("gbk"); //使用默认的字符集

// for(int i = 0;i

// System.out.println(arr[i]); // 101\101\108\108\111

// }

// byte[]——》String

String str2 = new String(arr);

System.out.println(str2); //中国

}

}

三、String中常用方法

1、boolean isEmpty():字符串是否为空; 2、int length():返回字符串的长度; 3、String concat(xx):字符串拼接; 4、boolean equals(Object obj):比较字符串是否相等,区分大小写; 5、boolean equalsIgnoreCase(Object obj):比较字符串是否相等,不区分大小写; 6、int compareTo(String other):比较字符串大小,区分大小写,按照Unicode编码值比较大小; 7、int compareTolgnoreCase(String other):比较字符串大小,不区分大小写; 8、String toLowerCase():将字符串中大写字母转为小写; 9、String toUpperCase():将字符串中小写字母转为大写; 10、String trim():去掉字符串前后空白符; 11、public String intern():结果在常量池中共享; 12、boolean contains(xx):是否包含xx 13、int indexOf(xx):从前往后找当前字符串中xx,即如果有返回第一次出现的下标,要是没有返回-1; 14、int indexOf(String str,int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引开始向后找; 15、int lastIndexOf(xx):从后往前找当前字符串中xx,即如果有返回最后一次出现的下标,要是没有返回-1 16、int lastIndexOf(String str,int fromIndex):返回指定子字符串在此字符串中最后一次出现处的并且向前找; 17、String substring(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取; 18、String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串从beginIndex开始截取,到endIndex结束; 19、char charAt(index):返回[index]位置的字符 20、char[] toCharArray(): 将此字符串转换为一个新的字符数组返回 21、static String valueOf(char[] data):返回char数组参数的字符串表示形式 22、static String valueOf(char[] data,int offset,int count): 返回char数组参数的特定子数组的字符串表示形式 23、static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的字符串。 24、static String copyValue0f(char[] data,int offset,int count):返回指定数组中指定片段的字符串。start:开始下标 count:长度 25、boolean startsWith(xx):方法用于检测字符串是否以指定的子字符串开始。 26、boolean startsWith(string prefix,int toffset):如果字符串以指定的前缀开始,则返回 true;否则返回 false。 27、boolean endsWith(xx):测试此字符串是否以指定的后结束 28、String replace(char oldchar,char newchar):返回一个新的字符串,它是通过用 newchar 替换oldchar; 29、String replace(CharSequence target,charSequence replacement):用replacement替换所有的target,两个参数都是字符串。 30、String replaceAll(String regex,String replacement):用replacement替换所有的regex匹配项,regex很明显是个正则表达式,replacement是字符串。 31、String replaceFirst(String regex,String replacement):基本和replaceAll相同,区别是只替换第一个匹配项。

相关推荐

麻豆传媒出了游戏
约彩365官方网站下载

麻豆传媒出了游戏

📅 09-30 👁️ 358
要不要取消军训,看完这篇文章就明白了
mobile28365正规网址

要不要取消军训,看完这篇文章就明白了

📅 08-03 👁️ 6883
清理手机垃圾最佳方法
365正规网站是多少

清理手机垃圾最佳方法

📅 10-18 👁️ 270