您好,欢迎来到画鸵萌宠网。
搜索
您的当前位置:首页Java小记——String类

Java小记——String类

来源:画鸵萌宠网


字符串概念

1.字符串是由一个或多个字符组成的一串数据(字符序列)
2.字符串可以看成是字符数组

String类的构造方法

1.空参构造

2.参数为字符串 //字符串常量值转成字符串

String s = new String("hello");

System.out.println(s);

结果:

hello

3.参数为字节数组 //把字节数组转成字符串

byte[] bytes = {97, 98, 99, 100};

String s = new String(bytes);

System.out.println(s)

结果:

abcd

把字节数组的一部分转成字符串

byte[] bytes = {97, 98, 99, 100};

String s1 = new String(bytes, 2, 2); //从2索引开始,取后两个转成字符串

System.out.println(s1);

结果:

cd

4.参数为字符数组 //把字符数组转成字符串

char[] chars = {'A', 'B', 'C', '我', '爱', '你'};

String string = new String(chars);

System.out.println(string);

结果:

ABC我爱你

把字符数组的一部分转成字符串

char[] chars = {'A', 'B', 'C', '我', '爱', '你'};

String s = new String(chars, 3, 3); //从3索引处开始,取后三个转成字符串
System.out.println(s);

结果:

我爱你

String类的特点

1.字符串字面值"abc"也可以看成是一个字符串对象。

"abc"                    //为一对象    引用存在字符串常量池中

String s1 = "abc"; //为一对象

2.字符串是常量,一旦被创建,就不能被改变。值不能被改变,你能改变的是引用或者说指向。

String s = "hello";
s = "world" + "java";

System.out.println(s);

结果:

worldjava

 3. 定义一个字符串时,会先在字符串常量池中找有没有这个字符串,若没有,则在堆中创建一个对象,在常量池中存放它的索引,若已存在这个字符串,则无需创建对象,在常量池中找到这个索引即可。

String s = new String("hello"); //创建两个对象

String s = "hello";  //创建一个对象

当两条语句一起出现时,也只创建两个对象,因为字符串常量池中已有hello的索引,无需再创建对象。

==和equals的区别

 ==是一个比较运算符,可以比较基本数据类型,也可以比较引用数据类型

比较基本数据类型,则比较两个变量的值是否相等

String类,也重写 equals() 方法,比较的是两个字符串,字面上的内容是否相同。

String类的判断方法

1.equals()     比较字符串的内容是否相同,区分大小写

System.out.println("abc".equals("abc"))

2.equalsIgnoreCase()     比较字符串的内容是否相同,忽略大小写

System.out.println("Abc".equalsIgnoreCase("aBc"))

3.contains()      判断字符串中是否包含传递进来的字符串

System.out.println("abc".contains("ab"))

4.startsWith()     判断字符串是否以传递进来的字符串开头

 System.out.println("abc".startsWith("a"));

5.endsWith      判断字符串是否以传递进来的字符串结尾

System.out.println("abc".endsWith("c"));

6.isEmpty       判断字符串的内容是否为空串""

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

String类的获取方法

1.length ()    获取字符串的长度

int len = "abc".length();

System.out.println(len);

2.charAt(index)      获取指定索引位置的字符

char ch = "abc".charAt(1);
System.out.println(ch);

3.indexOf(ch)       返回指定字符在此字符串中第一次出现处的索引

String s = "abc";
int index = s.indexOf('b');

System.out.println(index);

查不到就返回-1

4.indexOf(str)     返回指定字符串在此字符串中第一次出现处的索引

String s = "abc";
int index = s.indexOf("bc");

System.out.println(index);

查不到就返回-1

5.indexOf (str  index)    返回指定字符串在此字符串中从指定位置后第一次出现处的索引

String s = "返回指定字符回在此字符串中第一次出现处的索引一"

int i = s.indexOf("回", 2);
 System.out.println(i);

6.substring (start)     从指定位置开始截取字符串, 默认到末尾

String str = "从指定位置开始截取字符串, 默认到末尾";

String substring1 = str.substring(5); 
System.out.println(substring1);  //结果:开始截取字符串, 默认到末尾

7.substring ( start, end)   从指定位置开始到指定位置结束截取字符串

String str = "从指定位置开始截取字符串, 默认到末尾";
String substring = str.substring(0, 5); 
System.out.println(substring);  //结果:从指定位置

String类的转换方法

1.getBytes()    把字符串转换为字节数组

byte[] bytes = str.getBytes();
for (byte aByte : bytes) {
     System.out.println(aByte);
}

把字节数组转换为字符串

String s = new String(bytes);
System.out.println(s);

2.toCharArray()    把字符串转换为字符数组

char[] chars = s2.toCharArray()

把字符数组转换成字符串

String s1 = new String(chars);
System.out.println(s1);

3.valueOf(char[] chs/int i)     把任意类型的数据转成字符串

String s = String.valueOf(50);
String s1 = String.valueOf(3.25);

String s3 = String.valueOf(false);

String s4 = String.valueOf(new char[]{'A', 'B', 'C'});

4.toLowerCase()    把字符串转成小写

   toUpperCase()    把字符串转成大写

System.out.println("abc".toUpperCase());
System.out.println("ABC".toLowerCase());

5.concat(str)      字符串拼接

String concat = "你好".concat("世界").concat("爱生活").concat("爱Java");

System.out.println(concat);

6.replace(char old , char new)      将指定字符进行互换

String str = "abcd"

String s = str.replace('b', '*');
System.out.println(s);

7.replace(String old,String new)       将指定字符串进行互换

String str = "abcd"

String s = str.replace("abc", "df");
System.out.println(s);

8.trim()    去除两端空格

String ss = "    呵  呵    ";
String trim = ss.trim();

System.out.println(trim);

9.compareTo(str)   顺序比较两个字符串  相同返回0 前一个多返回1 后一个多返回-1

String s1 = "abc";
String s2 = "abcd";

int i = s1.compareTo(s2);
System.out.println(i);
打印 -1

String s1 = "abc";
String s2 = "abc";

int i = s1.compareTo(s2);
System.out.println(i);

打印0

String s1 = "abcd";
String s2 = "abc";

int i = s1.compareTo(s2);
System.out.println(i);

打印1

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo8.com 版权所有 湘ICP备2023022238号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务