String类:
String常用方法简介:
创建String对象的方法:
方法测试:
package com.imooc.string;
public class StringDemo1 {
public static void main(String[] args) {
// 定义一个字符串
String str = "JAVA 编程 基础";
// length()方法
System.out.println("字符串str的长度是:" + str.length());
// charAt(int index)方法
// 取出字符 '程'
System.out.println(str.charAt(6));
// String substring(int beginINdex)
// String substring(int beginINdex,int endIndex),
// 输出字符串下标在[beginINdex,endIndex)的字符串
// 取出字符串 '编程 基础'
System.out.println(str.substring(5));
System.out.println(str.substring(5, 10));
System.out.println("==============");
String str1 = new String("JAVA编程基础,我喜欢java编程");
System.out.println("字符串str的长度是:" + str1.length());
// indexOf(char ch)//查找 'ch' 在字符串中第一次出现的位置;
System.out.println("字符 'A' 在字符串中第一次出现的位置为:" + str1.indexOf('A'));
// indexOf(String str) 查找 'str' 在字符串中第一次出现的位置;
System.out.println("字串\"编程\" 在字符串中第一次出现的位置为:" + str1.indexOf("编程"));
// indexOf(String str,int fromIndex) 从字符串index为8的位置开始,
//查找 'str' 在字符串中第一次出现的位置;
System.out.println("从字符串index为8的位置开始,字串\"编程\" 在字符串中第一次出现的位置为:" + str1.indexOf("编程",8));
//
// lastIndexOf(char ch)//查找 'ch' 在字符串中最后一次出现的位置;
System.out.println("字符 'A' 在字符串中最后一次出现的位置为:" + str1.lastIndexOf('A'));
// lastIndexOf(String str)//查找 'str' 在字符串中最后一次出现的位置;
System.out.println("字串\"编程\" 在字符串中最后一次出现的位置为:" + str1.lastIndexOf("编程"));
}
}
输出结果
字符串str的长度是:10
程
编程 基础
编程 基础
==============
字符串str的长度是:18
字符 'A' 在字符串中第一次出现的位置为:1
字串"编程" 在字符串中第一次出现的位置为:4
从字符串index为8的位置开始,字串"编程" 在字符串中第一次出现的位置为:16
字符 'A' 在字符串中最后一次出现的位置为:3
字串"编程" 在字符串中最后一次出现的位置为:16
编程练习:
package test008;
public class StringDemo {
public static void main(String[] args) {
// 已知字符串"abcdefg",
// 使之经过处理得到字符串"CDE",
// 并进一步 将“DE”替换为“NM”,得到结果:CNM
String str = "abcdefg";
str = str.substring(2, 5).toUpperCase().replace("DE", "NM");
System.out.println(str);
}
}
输出结果:
CNM
字符串方法equals()和compareTo()的区别,方法equels()比较两个字符串内容是否相等,返回值是boolean类型的值;方法compareTo()比较两个字符串大小,返回值是int的值
字符串与byte数组的相互转换:
测试代码:
package com.imooc.string;
import java.io.UnsupportedEncodingException;
public class StringDemo2 {
public static void main(String[] args) throws UnsupportedEncodingException {
// 字符串和byte数组之间的转换
String str = "JAVA 编程 基础";
//将字符串转为byte数组,并打印输出
byte[] arrs = str.getBytes();
for (int i=0;i<arrs.length;i++) {
System.out.print(arrs[i]+" ");
}
System.out.println();
//将byte数组转为字符串,并打印输出
String str1 = new String(arrs);
System.out.print(str1);
}
}
输出结果:
74 65 86 65 32 -25 -68 -106 -25 -88 -117 32 -27 -97 -70 -25 -95 -128
JAVA 编程 基础
== 运算符 与 equals 方法的区别:
== :用来判断地址是否相同
equals :用来判断内容是否相同
测试代码:
package com.imooc.string;
public class StringDemo3 {
public static void main(String[] args) {
// == 和 equals 方法的区别
// 定义三个字符串,内容都是 "imooc"
String str1 = "imooc";
String str2 = "imooc";
String str3 = new String("imooc");
System.out.println("str1和str2的内容是否相同?" + str1.equals(str2));
System.out.println("str1和str3的内容是否相同?" + str1.equals(str3));
System.out.println("str1和str2的地址是否相同?" + (str1 == str2));
System.out.println("str1和str3的地址是否相同?" + (str1 == str3));
}
}
输出结果:
str1和str2的内容是否相同?true
str1和str3的内容是否相同?true
str1和str2的地址是否相同?true
str1和str3的地址是否相同?false
字符串的不可变动性:
测试代码:
package com.imooc.string;
public class StringDemo4 {
public static void main(String[] args) {
// String的不可变性
// String 对象一旦被创建,则不能修改,是不可变的
// 所谓的修改其实是创建了新的对象,所指向的内存空间不变
String s1 = "imooc";
System.out.println(s1);
System.out.println("s1的内存地址"+s1.hashCode());
System.out.println("=================");
String s2 = "imooc";
System.out.println(s2);
System.out.println("s2的内存地址"+s2.hashCode());
System.out.println("=================");
//s1不再指向imooc所在的空间,而是指向了"hello imooc"所在的空间
s1 = "hello "+s1;
System.out.println(s1);
System.out.println("改变后s1的内存地址"+s1.hashCode());
String s3 = new String("hello imooc");
System.out.println("s3的字串:"+s3.substring(0,5));//hello存放在堆中
System.out.println("s3:"+s3);
}
}
实际进行的操作:
输出结果:
imooc
s1的内存地址100327135
=================
imooc
s2的内存地址100327135
=================
hello imooc
改变后s1的内存地址1781114385
s3的字串:hello
s3:hello imooc
StringBuilder类:
String与StringBuilder的区别:
常用方法:
具有可变性:
测试代码:
package com.imooc.string;
public class StringBuilderDemo1 {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("你好");
System.out.println("str=" + str);
System.out.println("str的地址为:" + str.hashCode());
str = str.append(",imooc");
System.out.println("str=" + str);
System.out.println("str的地址为:" + str.hashCode());
// 将字符串变为你好,IMOOC
// 两种方式:
// 方案1、用delete删除mooc,然后再插入MOOC
System.out.println("删除后:str=" + str.delete(4, 8).insert(4, "MOOC"));
// 方案2、用replace方法直接替换
System.out.println("替换后:str=" + str.replace(4, 8, "MOOC"));
//将字符串中的"你好"取出并输出
System.out.println(str.substring(0, 2));
}
}
输出结果:
str=你好
str的地址为:2018699554
str=你好,imooc
str的地址为:2018699554
删除后:str=你好,iMOOC
替换后:str=你好,iMOOC
你好
编程练习:
package com.imooc.test;
public class StringTest {
public static void main(String[] args) {
//定义一个字符串"欢迎来到"
StringBuilder str = new StringBuilder("欢迎来到");
//在"欢迎来到"后面添加内容,将字符串变成"欢迎来到imooc"
str = str.append("imooc");
System.out.println(str);
//使用delete方法删除"欢迎来到",然后插入“你好!”
str = str.delete(0,4).insert(0,"你好,");
System.out.println("删除插入后:"+str);
//将字符串变成“你好,imooc”
//使用replace方法替换
str = str.replace(0, 3, "欢迎来到");
System.out.println("替换后:"+str);
}
}
输出结果:
欢迎来到imooc
删除插入后:你好,imooc
替换后:欢迎来到imooc
总结: