J学习日志12(2)-字符串



一.字符串

1.“==” 用于比较引用数据类型的时候比较的是两个对象的内存地址;
2.equals方法默认情况下比较也是两个对象的内存地址。String类除外,String类重写了equals方法,比较的是两个字符串对象的内容是否一致。实际开发,比较String类时,建议使用equals方法。
3.使用equals方法时候一般都会让常量作为方法的调用者。

public class Demo2 {
	public static void main(String[] args) {
		String str1="hello";
		String str2="hello";
		String str3=new String("hello");
		String str4=new String("hello");
		
		System.out.println("str1==str2"+(str1==str2));
		System.out.println("str2==str3"+(str2==str3));
		System.out.println("str3==str4"+(str3==str4));
		System.out.println("str3.equals(4)?"+(str3.equals(str4)));
		
		test(null);
	}
	
	public static void test(String str) {
		//3.使用equals方法时候一般都会让常量作为方法的调用者
		if("中国".equals(str)) {
			System.out.println("回答正确");
		}else {
			System.out.println("回答错误");
		}		
	}
}

str1==str2true
str2==str3false
str3==str4false
str3.equals(4)?true
回答错误

二.Stringbuffer存储字符的容器

1.String字符串的特点
(1)字符串是常量,它们的值在创建之后不能更改。
(2)字符串的内容一旦发生了变化,那么马上回创建一个新对象。

public class Demo4 {
	public static void main(String[] args) {
		String str1="hello";
		String str2=str1+" world";
		System.out.println("str1与str2是同一个对象吗?"+(str1==str2));	
	}
}
str1与str2是同一个对象吗?false

2.StringBuffer与StringBuilder
(1)如果需要频繁修改字符串的内容,建议使用StringBuffer
(2)使用StringBuffer无参的构造函数创建一个对象时,默认的容量是16,超过就自动增长1倍。

StringBuffer与StringBuilder的区别
1.相同点:
(1)两个类都是字符串缓冲类。
(2)两个类的方法都是一致的。
2.不同点:
(1)StringBuffer是线程安全的,操作效率低,StringBuilder非安全,效率高
(2)StringBuffer是jdk1.0出现,StringBuilder是jdk1.5出现
推荐使用:StringBuilder因为效率高。

public class Demo4 {
	
	public static void main(String[] args) {
		StringBuffer sb=new StringBuffer();
		sb.append("abc");
		sb.append(3.14);
		System.out.println(sb);		
	}
}
abc3.14
public class Demo4 {
	public static void main(String[] args) {
		StringBuffer sb=new StringBuffer();
		sb.append("abc");
		sb.append(3.14);
		sb.reverse();//倒序输出,abc,cba
		System.out.println(sb);		
	}
}
41.3cba

三.字符串练习

1.实现自己的trim的方法:

public class Demo3 {	
	public static void main(String[] args) {
		String str="    创智  哈哈  ";
		System.out.println(myTrim(str));	
	}

	public static String myTrim(String str) {
		//先转换字符数组
		char[] arr=str.toCharArray();
		//定义两个变量 记录开始的索引值与结束的索引值
		int startIndex=0;
		int endIndex=arr.length-1;
		while(true) {
			if(arr[startIndex]==' ') {
				startIndex++;
			}else {
				break;
			}			
		}
		
		while(true) {
			if(arr[endIndex]==' ') {
				endIndex--;
			}else {
				break;
			}			
		}
		
		//返回截取到的值,使用 substring() 从字符串中提取一些字符
		return str.substring(startIndex, endIndex+1);		
	}	
}

2.获取上传文件名“D:\20210217\day10\Demo1.java”。

public class Demo3 {	
	public static void main(String[] args) {		
		str="D:\\20210217\\day10\\Demo1.java";
		getFileName(str);	
	}	
	public static void getFileName(String path) {
		int index=path.lastIndexOf("\\");
		String fileName=path.substring(index+1);
		System.out.println("文件名:"+fileName);	
	}
}

3.将字符串对象中存在的字符反序。

public class Demo3 {
	public static void main(String[] args) {	
		str="祝祖国繁荣昌盛";
		System.out.println(reverse(str));		
	}
	
	public static String reverse(String str) {
		char[] arr=str.toCharArray();
		for(int startIndex=0,endIndex=arr.length-1;startIndex<endIndex;startIndex++,endIndex--) {
			char temp=arr[startIndex];
			arr[startIndex]=arr[endIndex];
			arr[endIndex]=temp;			
		}
		//使用字符数组构建一个字符串。
		return new String(arr);
	}

4.统计子串出现的次数

public class Demo3 {	
	public static void main(String[] args) {	
		str="dsjavahapahapjav";
		getCount(str,"java");
	}

	//统计字串出现的次数
	public static void getCount(String str,String target) {		
		int count=0;//用于记录出现的次数
		int fromIndex=0;//从哪个索引值开始寻找目标子串
		
		while((fromIndex=str.indexOf(target,fromIndex))!=-1 ){
			//如果indexof方法返回的不是-1,那么就是已经找到了目标元素
			count++;
			fromIndex=fromIndex+target.length();			
		}
		System.out.println("出现次数:"+count);		
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值