Java基础之 String类中的常用方法

	String str = "HelloWorld";
	String st = "helloworld";
	String si="";
	//---------------Strig 类中的判断方法---------------------------------------------
	//1.String  equals() 判断内容是否相等 区分大小写
	System.out.println(str.equals(st));
	// 2.equals Ignore Case();  不区分大小写
	 System.out.println(str.equalsIgnoreCase(st));
	//3.Contains 是否包含某个字符串 有大小写区分
	 //  Contains()  里面是询问str里含有字符串
	 System.out.println(str.contains("w"));
	 //4.isEmpty() 判断是否为空
	 System.out.println(si.isEmpty());
	 //5.startsWith()  判断是否以该字符为前缀
	 System.out.println(str.startsWith("H"));
	 //6.endsWith()  判断是否以该字符为后缀
	 System.out.println(str.endsWith("HelloWorld"));
	 //7.contentEquals 判断该字符与StringBuffer的内容是否相等
	 
	 StringBuffer sb = new StringBuffer();
	 //append() 链接
	 sb.append("Hello");
	 sb.append("World");
	 sb.append("!");
	 System.out.println(sb);
	 System.out.println(str.contentEquals(sb));


	//----------------------String 用来获取的功能--------------------------


 //1.获取字符串长度 空格算一个长度
	
	/*  length 属性
	 *  length() 方法
	 *  size()  方法  
	*/
	System.out.println(si.length());
//2.charAt() 获取索引对应的字符
	//索引的下标是从0开始
	System.out.println(st.charAt(1));
//3.indexOf() 判断该字符在字符串中位置
	//该字符在字符串中第一次出现的下标位置
	System.out.println(st.indexOf("l")+"---");
//4.subString  截取
	//从5的位置进行截前面留后面含5的
	System.out.println(st.substring(5));
	// hel low orld
	//截取的是3-5的上字符
	System.out.println(st.substring(3, 6));
//5.copyValueOf  将字符数组转换成字符串
char[] ch = {'a','b','c','d','f'};
System.out.println(si.copyValueOf(ch));
//int offset  从哪个位置开始且该位置上的字符
//int count    后移个数
//String (char[] ch ,int offset ,int count)
System.out.println(si.copyValueOf(ch, 1, 3));
			 
	//-----------------String 类转换功能-------------------------------
	String str = "HelloWorld";
	String st = "helloworld";
	String si="";

//1.getBytes  用来转换成byte数组
	System.out.println(str.getBytes());  //得到的是内存地址
	
  byte[] by = str.getBytes();
  for (int i = 0; i < by.length; i++) {
	 //  System.out.println(by[i]);   
}
//2.tocharArray  用来转换成字符数组
  char[] ch = str.toCharArray();
 // System.out.println(ch);
  for (int i = 0; i < ch.length; i++) {
 System.out.println(ch[i]);
 }
 //3.valueOf 将基本数据类型转换成字符 
 boolean i = true;    
 String sing = String.valueOf(i);
 System.out.println(sing);
 //4.toLowerCase  转换成小写
 String sin ="HelloWorld";
System.out.println(sin.toLowerCase()) ;
//5.toUpperCase 转换成大写
String sg ="HelloWorld";
System.out.println(sg.toUpperCase());
 //6.concat 拼接 相当于+ 但是concat会开辟新的内存空间
String s1="hello";
String s2="world";
String s3=s1+s2;
String s4= s1.concat(s2);
System.out.println(s4);
/-----------------String 类其他功能(会用到的)--------------------------------
//1. replace 替换功能
 System.out.println(str.replace('l', 'a')); 
 System.out.println(str.replace("Hello", "a"));

 //2.trim 去首尾空格
 System.out.println("***"+str.trim()+"****");
 
 //3.compareTo  对比两个字符串大小排序
 //前面大于后面是正整数
 //前面后面相等是 0
 //前面小于后面是负整数
 //如果第一位不同则比较第一位
 //如果第一位相同则比较后以为
 System.out.println(st.compareTo(str)); 

}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值