从零双排学java之String字符串类(系统类)

                                    字符串类

字符串类属于系统类

需要注意:字符串  是字符串常量 存在于方法区的常量池中

如何学习:系统类的方法

1.有没有关键字  static  修饰

2.返回值的类型

3.方法名,一般方法名都是见名知意的

4.参数列表(看方法需要什么)

String s1="abc";  这种创建形式 会先去静态区中找有没有这样的一个字符串  

如果有地址就会指向这个字符串 ,没有就会创建一个

String s3=new String("abc");    会在堆中生成一个对象  在指向栈中的变量名的同时,

也指向静态区的字符串


//修改了 字符串引用(地址)
	//指向了不同的地址  而不是字符串本身被修改了
	//看系统方法时  拼接 截取等操作字符串的一类方法
	//都是返回一个新的字符串
	String s1="abc";
	s1="wanglong";
	System.out.println(s1);
	
	//s2相当于在方法区的常量池中创建了一个字符串
	String s2="abc";
	//s3是在堆内存中 开辟一块空间
	String s3=new String("abc");
	String s4="abc";
	//==对象比的是地址
	System.out.println(s2==s4);
	//equals 把两个字符创转换成字符数组  一位一位的进行比较
	System.out.println(s2.equals(s4));
	System.out.println(s2==s3);
	//s2和s3 有什么区别?
	//s2 是一个对象  s3  是两个对象  "abc"    和  new出来的对象
这里列举了一些常用的字符串方法

/* 常用的方法 下面列举了一部分
 * 替换 切割 获取子串 转换大小写 去空格 比较
	判断两个字符串相等(忽略大小写)
	把字符数组 转化为 字符串
	把字符串转化为 字符数组
	判断字符串是否为空
 */
public class Demo03 {
	public static void main(String[] args) {
		// fun1();
		// fun2();
		// fun3();
		// fun4();
		// fun5();
		// fun6();
		// fun7();

	}

	private static void fun7() {
		// 基本数据类型转换成字符串
		String valueOf = String.valueOf(1000);
		System.out.println(valueOf);
	}

	private static void fun6() {
		// 判断字符串是否为空
		String string = "";
		boolean empty = string.isEmpty();
		System.out.println(empty);
		// 字符串拼接
		String s1 = "wan";
		String s2 = "long";
		String s3 = s1 + s2;
		System.out.println(s3);
		String concat = s1.concat(s2);
		System.out.println(concat);
	}

	private static void fun5() {
		// 把字符数组转化成字符串
		char[] array = { 'w', 'a', 'n', 'l', 'o', 'n', 'g' };
		String string = new String(array);
		System.out.println(string);
		// 字符串转化成字符数组
		String s1 = "wanglong";
		char[] charArray = s1.toCharArray();
		for (char c : charArray) {
			System.out.println(c);
		}
	}

	private static void fun4() {
		// 去空格
		String string = " abc def   ";
		String trim = string.trim();
		System.out.println(trim);
		// 字符串比较
		String s2 = "abcAB";
		String s3 = "Abc";
		// 相等返回的是0 返回正值说明前面大 负值前面小
		// 字符不一样的时候 按ASCII表 返回两个字符的差值
		// 长度不一样的时候 返回的是位数的 差值
		// 一位一位比较 字符不一样就做差值
		int compareTo = s2.compareTo(s3);
		System.out.println(compareTo);
	}

	private static void fun3() {
		// 获取子字符串
		String string = "wanglong";
		// 从传入的索引这一位开始截取(包括3)
		String substring = string.substring(3);
		System.out.println(substring);
		// [0,3)留头不留尾 实际[0,2]
		String substring2 = string.substring(0, 3);
		System.out.println(substring2);
	}

	private static void fun2() {
		// 分割
		String string = "wanglong.pengqian.liushankun";
		// 使用转义符
		String[] split = string.split("\\.");
		// 增加for循环(专门用来遍历的)
		for (String sds : split) {
			// String sds 表示容器中的每一个元素
			System.out.println(sds);
		}
	}

	private static void fun1() {
		String string = "wanglong";
		// 替换
		String replace = string.replace("w", "l");
		System.out.println(replace);
		// 替换字符串
		String replace2 = string.replace("long", "ba");
		System.out.println(replace2);
	}
}


猜你喜欢

转载自blog.csdn.net/jsymax/article/details/80342467